bash 如何从“时间”命令中获取实时值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3795470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:41:57  来源:igfitidea点击:

How do I get just real time value from 'time' command?

bashscriptingshell

提问by Matthew Sowders

I would like to make a script that outputs only the real time value from the time command so I can plot the results. For example time commandoutputs

我想制作一个脚本,只输出 time 命令的实时值,以便我可以绘制结果。例如time command输出

real    1m0.001s
user    1m0.000s
sys  0m0.001s

I want to write a script that outputs

我想写一个输出的脚本

60.001 

How do I get just real time value from 'time' command in seconds?

如何以秒为单位从“时间”命令中获取实时值?

回答by Paused until further notice.

If you're using the Bash builtin time, set the TIMEFORMATvariable to %R:

如果您使用的是 Bash 内置time,请将TIMEFORMAT变量设置为%R

$ TIMEFORMAT=%R
$ time sleep 1
1.022

回答by eumiro

timecan take an optional --formator -fparameter, but you have to use the full path to the time command, /usr/bin/time

time可以采用可选--format-f参数,但您必须使用时间命令的完整路径,/usr/bin/time

/usr/bin/time -f "%e" sleep 3

3.00

Without the path, it'll use the timecommand of the shellwhich just treats all arguments as the command, so you'll get an -f: command not found.

如果没有路径,它将使用timeshell的命令,该命令仅将所有参数视为命令,因此您将获得一个-f: command not found.

回答by Martin T.

If you write \timeyou enforce not to use the bash built in time. So with \time -f '%e' commandyou are done.

如果你写\time你强制不使用内置的 bash 时间。这样\time -f '%e' command你就完成了。

回答by Archimedix

Alternatively, use /usr/bin/timeor take a look at the similar question
Using time command in bash script.

或者,使用/usr/bin/time或查看类似问题
Using time command in bash script