Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
Linux 如何将bash管道与perl脚本一起使用_Linux_Perl - Fatal编程技术网

Linux 如何将bash管道与perl脚本一起使用

Linux 如何将bash管道与perl脚本一起使用,linux,perl,Linux,Perl,我需要使用系统使用perl脚本运行此命令。此代码行不起作用。您知道为什么吗 system("ping -w 300 -i $interval $host \| sed 's/\(.*\)/\$(date +%F\ %T) \1/g' >> $ test" ) 使用这个代码行,我能够得到我所需要的您的程序充满了代码注入错误 在使用夏令时的地方,使用localtime作为时间戳而不包括时区信息(例如,与UTC的偏移量)是有问题的 以下是解决这些问题的解决方案: use POSIX

我需要使用系统使用perl脚本运行此命令。此代码行不起作用。您知道为什么吗

system("ping -w 300  -i $interval $host \|  sed 's/\(.*\)/\$(date +%F\ %T) \1/g' >> $ test" )

使用这个代码行,我能够得到我所需要的

您的程序充满了代码注入错误

在使用夏令时的地方,使用localtime作为时间戳而不包括时区信息(例如,与UTC的偏移量)是有问题的

以下是解决这些问题的解决方案:

use POSIX              qw( strftime );
use String::ShellQuote qw( shell_quote );

open(my $fh_log, '>>', $test)
   or die $!;

open(my $pipe, '|-', "ping", "-w", "300", "-i", $interval, "--", $host)
   or die $!;

while (<$pipe>) {
   my $ts = strftime("%Y-%m-%dT%H:%M:%S%z", localtime);
   print($fh_log "[$ts] $_");
}
使用POSIXQW(strftime);
使用字符串::ShellQuote qw(shell_quote);
打开(我的$fh_日志,'>>',$test)
或者死美元!;
打开(my$pipe、“|-”、“ping”、“-w”、“300”、“-i”、$interval、“-”、$host)
或者死美元!;
而(){
my$ts=strftime(“%Y-%m-%dT%H:%m:%S%z”,localtime);
打印($fh_日志“[$ts]$);
}

报价将是您的问题。只需使用
Net::Ping
并在
perl
中执行工作,“此代码行不工作”并不是对问题的充分描述。哇,这看起来很糟糕(可能会受到命令注入的影响)。
use POSIX              qw( strftime );
use String::ShellQuote qw( shell_quote );

open(my $fh_log, '>>', $test)
   or die $!;

open(my $pipe, '|-', "ping", "-w", "300", "-i", $interval, "--", $host)
   or die $!;

while (<$pipe>) {
   my $ts = strftime("%Y-%m-%dT%H:%M:%S%z", localtime);
   print($fh_log "[$ts] $_");
}