加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash正则表达式字符串变量匹配

发布时间:2020-12-15 22:11:04 所属栏目:安全 来源:网络整理
导读:我在perl中编写了以下脚本,工作得很好.但我试图用bash实现同样的目的. #!/usr/bin/perluse 5.010;use strict;INIT { my $string = 'Seconds_Behind_Master: 1'; my ($s) = ($string =~ /Seconds_Behind_Master: ([d]+)/); if ($s 10) { print "Too long...
我在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读