perl – 如何使用反引号来捕获从时间(1)输出的经过时间?

前端之家收集整理的这篇文章主要介绍了perl – 如何使用反引号来捕获从时间(1)输出的经过时间?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用以下命令从 time获取输出.我唯一想要的是获得经过的时间,但我一无所获.

$result = `/usr/bin/time -f %e "./"$PROG > /dev/null`;
print $result;

解决方法

所需的重定向只是有点棘手.

my $result = `/usr/bin/time -f %e "./"$PROG 2>&1 >/dev/null`;

time输出其对标准错误(文件描述符2)的测量值,但Perl反引号捕获标准输出(文件描述符1).读取上面的重定向序列为“首先将标准错误发送到标准输出的目标,然后丢弃原始标准输出.”

Perl FAQ中对How can I capture STDERR from an external command?的回答解决了您所看到的问题.

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

06001

… Ordering is important in all these examples. That’s because the shell processes file descriptor redirections in strictly left to right order.

06002

The first command sends both standard out and standard error to the temporary file. The second command sends only the old standard output there,and the old standard error shows up on the old standard out.

请注意,$result将包含一个尾随换行符,您可以使用chomp将其删除

chomp $result;

或全部在一行中

chomp(my $result = `/usr/bin/time -f %e "./"$PROG 2>&1 >/dev/null`);

猜你在找的Perl相关文章