我在perl中编写了以下脚本,效果很好。但是我正在尝试使用bash实现相同的目的。
#!/usr/bin/perl
use 5.010;
use strict;
INIT {
my $string = 'Seconds_Behind_Master: 1';
my ($s) = ($string =~ /Seconds_Behind_Master: ([\d]+)/);
if ($s > 10) {
print "Too long... ${s}";
} else {
print "It's ok";
}
}
__END__
如何使用bash脚本实现此目的?基本上,我希望能够读取和匹配字符串“Seconds_Behind_Master:N”末尾的值,其中N可以是任何值。 最佳答案
您可以为此使用工具sed
如果您想使用正则表达式:
#!/bin/sh
string="Seconds_Behind_Master: 1"
s=`echo $string | sed -r 's/Seconds_Behind_Master: ([0-9]+)/\1/g'`
if [ $s -gt 10 ]
then
echo "Too long... $s"
else
echo "It's OK"
fi