查询Perl输出

时间:2019-05-31 14:39:19

标签: perl

我需要在输出中看到的第二行进行查询。我需要检查命令是否在“ Listener LISTENER is running on”上返回并提供所需的输出。我的代码能够读取我需要验证的第一行而不是第二行。请指教。

添加了While语句,因此它可以读取第二行,但不起作用。

use strict;
use warnings;
my $cmd="srvctl status listener";
my $listenerstatus0;
my $msg0;
my $msg1;
open(Row1Stat,"$cmd |") || die ("Could not read the pipe\n");
$listenerstatus0 = <Row1Stat>;
close(Row1Stat);
if( $listenerstatus0 =~ m/Listener LISTENER is running/)
{
$msg0="LISTENER is running";
$msg1=1
}
elsif ($listenerstatus0 =~ m/Listener LISTENER is not running/) {
$msg0 = "LISTENER is not running";
$msg1 = 0;
}
else {
$msg0 = "Unable to Query LISTENER Status";
$msg1 = 0;
}

print "\nStatistic.Name1:$msg1";
print "\nMessage.Name1:$msg0";

下面是命令的输出,我需要检查第二行。

srvctl status listener
Listener LISTENER is enabled
Listener LISTENER is running on node(s): XYZ

脚本需要检查“ Listener LISTENER正在运行”,并使用脚本中定义的退出代码退出。

1 个答案:

答案 0 :(得分:1)

如果您想阅读两行,则不应该在第一行之后停止阅读:

open(Row1Stat,"$cmd |") || die ("Could not read the pipe\n");
$ignorethis= <Row1Stat>;       # Read 1st line
$listenerstatus0 = <Row1Stat>; # Read 2nd line
close(Row1Stat);