加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 06:06:32 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用以下命令从 time 获取输出.我唯一想要的是获得经过的时间,但我一无所获. $result = `/usr/bin/time -f %e "./"$PROG /dev/null`;print $result; 解决方法 所需的重定向只是有点棘手. my $result = `/usr/bin/time -f %e "./"$PROG 21 /dev/nul
我正在尝试使用以下命令从 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`);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读