Chinaunix

标题: 如何从执行的shell指令返回结果中判断是否含有某些字符串 [打印本页]

作者: 草中宝    时间: 2015-08-28 18:01
标题: 如何从执行的shell指令返回结果中判断是否含有某些字符串
本帖最后由 草中宝 于 2015-08-28 18:13 编辑

今天下午第一次用Perl,实现了连接MySQL、端口是否打开判断,但如何从执行的shell指令返回结果中判断是否含有某些字符串,这个对现在的我来说,实在太难,还请各位高手援手。

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:a4:6f:b6 brd ff:ff:ff:ff:ff:ff
    inet 90.0.12.83/16 brd 90.0.255.255 scope global eth0
    inet 90.0.12.78/32 scope global eth0
    inet 90.0.12.79/32 scope global eth0
    inet6 fe80::a00:27ff:fea4:6fb6/64 scope link
       valid_lft forever preferred_lft forever

perl代码如下
#!/usr/bin/perl -w
$ip_cmd_result = `ip a`;

print $ip_cmd_result;

如何判断"ip a"指令返回结果中含有"inet"和"/32 scope global eth0"的行呢,如有则把该行对应的IP打印出来,对于本例是90.0.12.78和90.0.12.79。

本人第一次来Perl板块,以前都不知道Perl有如此强大的功能,惭愧啊。

作者: yybmsrs    时间: 2015-08-28 18:18
  1. my $ip_cmd_result = '    inet 90.0.12.78/32 scope global eth0';

  2. if($ip_cmd_result =~ /\binet\b.+?(\d+\.\d+\.\d+\.\d+)\/32 scope global eth0/) {
  3.        print $1;
  4. }
复制代码
这是最简单的正则,稍微看下书就明白了
作者: jason680    时间: 2015-08-28 18:48
本帖最后由 jason680 于 2015-08-28 18:48 编辑

回复 1# 草中宝

my @aRet;
foreach(`ip a`){
  if(m|inet\s+((\d+[.]){3}\d+)/32 scope global eth0|){
    push @aRet, $1;
  }
}
if(scalar @aRet == 0){
  print "didn't get ip with /32 for eth0\n"
}
else{
  print "@aRet\n";
}

   
作者: 草中宝    时间: 2015-08-31 08:28
yybmsrs 发表于 2015-08-28 18:18
这是最简单的正则,稍微看下书就明白了


谢谢指导。
作者: 草中宝    时间: 2015-08-31 08:30
jason680 发表于 2015-08-28 18:48
回复 1# 草中宝

my @aRet;


感谢啊,还用不同的颜色标注文字,真是有心了。
作者: tc1989tc    时间: 2015-08-31 12:54
回复 3# jason680


    不明白为啥perl解析器还能指向shell的指令
作者: jason680    时间: 2015-08-31 14:36
回复 6# tc1989tc


内核源码都不是问题...
那Perl的源代码一看便知...

Perl source code
http://www.cpan.org/src/
   
作者: tc1989tc    时间: 2015-08-31 14:51
回复 7# jason680


    谢谢 还没那么深 不过以后可以看
我还在入门阶段
作者: jason680    时间: 2015-08-31 15:03
回复 8# tc1989tc


$ perldoc perlop
NAME
    perlop - Perl operators and precedence

DESCRIPTION
  Operator Precedence and Associativity
    Operator precedence and associativity work in Perl more or less like they
    do in mathematics.

    ....

    qx/STRING/
    `STRING`
        A string which is (possibly) interpolated and then executed as a
        system command with "/bin/sh" or its equivalent.
Shell wildcards,
        pipes, and redirections will be honored. The collected standard output
        of the command is returned;
standard error is unaffected. In scalar
        context, it comes back as a single (potentially multi-line) string, or
        undef if the command failed. In list context, returns a list of lines
        (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or
        an empty list if the command failed.

        Because backticks do not affect standard error, use shell file
        descriptor syntax (assuming the shell supports this) if you care to
        address this. To capture a command's STDERR and STDOUT together:

            $output = `cmd 2>&1`;

        To capture a command's STDOUT but discard its STDERR:

            $output = `cmd 2>/dev/null`;

        To capture a command's STDERR but discard its STDOUT (ordering is
        important here):

            $output = `cmd 2>&1 1>/dev/null`;

        To exchange a command's STDOUT and STDERR in order to capture the
        STDERR but leave its STDOUT to come out the old STDERR:

            $output = `cmd 3>&1 1>&2 2>&3 3>&-`;



   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2