【发布时间】:2020-04-30 21:37:35
【问题描述】:
我尝试从 perl 中的 shell 脚本获取 rtt 结果(如 149.982/150.125/150.280/0.265 ms ), 现在 ping.sh 可以从 shell 脚本中检索 rtt 结果,但是如何返回结果以及如何在 perl 中从 shell 脚本中获取返回结果?
call.pl
my $answer= system (". /home/george/ping.sh;getrtt 8.8.8.8");
if($answer == 0)
{
exit($answer >> 8);
my $results = ##how to get the rtt results from ping.sh ???
}
ping.sh
getrtt()
{
if ping $ip -c 5 -t 5 | grep -oP '[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+ ms'
then
echo ##how to retrun the results (ping $ip -c 5 -t 5 | grep -oP '[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+\/[+-]?([0-9]*[.])?[0-9]+ ms')???
else
echo '1'
fi
}
【问题讨论】:
-
system不返回输出,而是成功/失败。你需要qx(“反引号”),或者“管道打开”,或者——最好的——几个好的模块之一——比如Capture::Tiny、IPC::System::Simple、IPC::Run3、IPC::Run。你能搜索一下 Stackoverflow 吗?这已经讨论了很多次次 -
另外,您能否查看在previous question 的评论中给出的链接——this page 回答了具体问题(关于使用 shell 脚本中定义的函数)