Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于条件终止perl中的命令_Perl_Shell - Fatal编程技术网

基于条件终止perl中的命令

基于条件终止perl中的命令,perl,shell,Perl,Shell,我有很多TNS名称,我必须使用TNSPing实用程序从中获取主机名和端口号 while (my $line = <$fh>) { chomp $line; print "TNS: $line\n"; my $output = `tnsping $line | grep -Eo "HOST=[A-Za-z0-9.\-]*com?|PORT=[0-9]+"`; print "$output\n"; prin

我有很多TNS名称,我必须使用TNSPing实用程序从中获取主机名和端口号

while (my $line = <$fh>) {
  chomp $line;
  print "TNS: $line\n";
  my $output = `tnsping $line | grep -Eo  "HOST=[A-Za-z0-9.\-]*com?|PORT=[0-9]+"`;
  print "$output\n";
  print "----------------\n\n";
}
对于大多数TNS条目,代码工作正常,但对于某些条目,
tnsping
命令是不退出,因此会被击中。我尝试手动运行其中一个条目,发现它们正在打印到标准输出,但由于某些原因,tnsping实用程序没有退出

现在我的问题是,无论
tnsping
实用程序是否退出,如何修改脚本,使其在获得主机和端口数据后移动到下一个条目

我目前的理解是,只有当
tnsping
实用程序退出时,grep才会工作(即,它不是连续的)


我也愿意接受任何替代方法。

我建议使用
IO::Select
can\u read
。我还建议-不要在perl中运行
grep
doit

大概是这样的:

while ( my $line = <$fh> ) {
   chomp $line;
   print "TNS: $line\n";

   my $pid = open( my $output, '-|', 'tnsping $line' );
   my $select = IO::Select->new($output);

   my $host;
   my $port;
   #check if the FH is readable, with a 5s timeout. 
   while ( $select->can_read(5) ) {
      my $line = <$output>; 
      $line =~ m/HOST\s*=\s*([A-Za-z0-9.\-]*com?)/ and $host = $1;
      $line =~ m/PORT\s*=\s*([0-9]+)/ and $port = $1;
   }
   close($output);
   print $host, "\n";
   print $port, "\n";
   print "$output\n";
   print "----------------\n\n";
}
while(我的$line=){
chomp$行;
打印“TNS:$line\n”;
my$pid=open(my$output,'-|','tnsping$line');
我的$select=IO::select->new($output);
我的主人;
我的$port;
#检查FH是否可读,超时5秒。
而($select->can_read(5)){
我的$line=;
$line=~m/HOST\s*=\s*([A-Za-z0-9.\-]*com?)和$HOST=$1;
$line=~m/PORT\s*=\s*([0-9]+)/和$PORT=$1;
}
关闭(产出美元);
打印$host“\n”;
打印$port,“\n”;
打印“$output\n”;
打印“-------------\n\n”;
}

我建议作为一个样式点-
$fh
不是一个很好的变量名

我建议使用
IO::Select
can\u read
。我还建议-不要在perl中运行
grep
doit

大概是这样的:

while ( my $line = <$fh> ) {
   chomp $line;
   print "TNS: $line\n";

   my $pid = open( my $output, '-|', 'tnsping $line' );
   my $select = IO::Select->new($output);

   my $host;
   my $port;
   #check if the FH is readable, with a 5s timeout. 
   while ( $select->can_read(5) ) {
      my $line = <$output>; 
      $line =~ m/HOST\s*=\s*([A-Za-z0-9.\-]*com?)/ and $host = $1;
      $line =~ m/PORT\s*=\s*([0-9]+)/ and $port = $1;
   }
   close($output);
   print $host, "\n";
   print $port, "\n";
   print "$output\n";
   print "----------------\n\n";
}
while(我的$line=){
chomp$行;
打印“TNS:$line\n”;
my$pid=open(my$output,'-|','tnsping$line');
我的$select=IO::select->new($output);
我的主人;
我的$port;
#检查FH是否可读,超时5秒。
而($select->can_read(5)){
我的$line=;
$line=~m/HOST\s*=\s*([A-Za-z0-9.\-]*com?)和$HOST=$1;
$line=~m/PORT\s*=\s*([0-9]+)/和$PORT=$1;
}
关闭(产出美元);
打印$host“\n”;
打印$port,“\n”;
打印“$output\n”;
打印“-------------\n\n”;
}

我建议作为一个样式点-
$fh
不是一个很好的变量名

您可以将超时添加到您的命令
timeout 20s您的命令
。此外,您还可以使用
命令&sleep 10;杀死美元。如果您的命令没有在10秒内完成,它将被杀死。您可以将超时添加到您的命令
timeout 20s您的命令
。此外,您还可以使用
命令&sleep 10;杀死美元。如果您的命令没有在10秒内完成,它将被杀死。