输入“time ./a.out”的输出到一个变量
我使用 time 命令来执行程序(如“xxx$ time ./a.out”),输出如下,
real 0m7.250s
usr 0m10.395s
sys 0m0.026s
我想要得到的是 0 和 7.250,如 0m7.250s。我尝试过“awk '{print $2}'”,但没有成功;那里没有任何输出。
PS:我尝试使用“>”将时间命令的输出放入文件,但也没有成功。
I have used time command to execute program (like "xxx$ time ./a.out"), with output as follows,
real 0m7.250s
usr 0m10.395s
sys 0m0.026s
What I want to get is 0 and 7.250 as in 0m7.250s. I have tried "awk '{print $2}'", but without success; nothing output there.
PS: I have tried put output of time command to a file by using ">", also without success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:(
当然,替换您自己的
a.out
命令,sleep 1
仅用于示例)。它为
time
命令创建一个子 shell,并确保其标准错误被发送到标准输出(time
特别将其信息输出到标准 error这样它就可以与程序的正常输出分开)。awk
命令捕获以real
开头的行并输出第二个参数(时间)。Try:
(substituting your own
a.out
command of course,sleep 1
was just used for an example).It creates a subshell for the
time
command and ensures that its standard error is sent to standard output instead (time
specifically outputs its information to standard error so that it's kept separate from normal output of the program it's timing).The
awk
command the captures the line startingreal
and outputs the second argument (the time).time
命令将其输出发送到标准错误,以免干扰正常的程序输出。您将需要使用2>&1
将其重定向到可以捕获的位置。The
time
command sends its output to standard error, so as not to interfere with normal program output. You will want to use2>&1
to redirect it where it can be captured.