时间命令的自定义格式
我想在 bash 脚本 中使用 time 命令来计算脚本的运行时间并将其写入日志文件。我只需要实时,而不是用户和系统。还需要一个合适的格式。例如 00:00:00:00 (与标准输出不同)。我很感激任何建议。
预期的格式应该是 00:00:00.0000
(毫秒)[小时]:[分钟]:[秒].[毫秒]
我已经有 3 个脚本了。我看到了这样的例子:
{ time { # section code goes here } } 2> timing.log
但我只需要实时,而不需要用户和系统。还需要一个合适的格式。例如 00:00:00:00
(与标准输出不同)。
换句话说,我想知道如何将时间输出变成更容易处理的东西。
I'd like to use the time command in a bash script to calculate the elapsed time of the script and write that to a log file. I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). I appreciate any advice.
The expected format supposed to be 00:00:00.0000
(milliseconds) [hours]:[minutes]:[seconds].[milliseconds]
I've already 3 scripts. I saw an example like this:
{ time { # section code goes here } } 2> timing.log
But I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00
(not like the standard output).
In other words, I'd like to know how to turn the time output into something easier to process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
date
命令获取执行要计时的工作之前和之后的当前时间,并计算差值,如下所示:
$((...)) 可用于 bash 中的基本算术 - 注意:不要在减号 - 之前添加空格,因为这样可能被解释为命令行选项。
另请参阅:http://tldp.org/LDP/abs/html/arithexp.html< /a>
编辑:
此外,您可能还需要查看sed< /a> 从 time 生成的输出中搜索并提取子字符串。
编辑:
以毫秒计时的示例(实际上是纳秒,但此处截断为毫秒)。您的
date
版本必须支持%N
格式,并且bash
应支持大数字。免责声明:
我的原始版本说,
但这已被删除,因为它显然对某些人不起作用,而据报道新版本却有效。我不同意这一点,因为我认为你必须只使用剩余的部分,但被否决了。
选择适合你的。
You could use the
date
command to get the current time before and after performing the work to be timed and calculate the difference like this:Notes:
$((...)) can be used for basic arithmetic in bash – caution: do not put spaces before a minus - as this might be interpreted as a command-line option.
See also: http://tldp.org/LDP/abs/html/arithexp.html
EDIT:
Additionally, you may want to take a look at sed to search and extract substrings from the output generated by time.
EDIT:
Example for timing with milliseconds (actually nanoseconds but truncated to milliseconds here). Your version of
date
has to support the%N
format andbash
should support large numbers.DISCLAIMER:
My original version said
but this was edited out because it apparently did not work for some people whereas the new version reportedly did. I did not approve of this because I think that you have to use the remainder only but was outvoted.
Choose whatever fits you.
要使用 Bash 内置
time
而不是/bin/time
,您可以设置此变量:它将输出如下所示的实时时间:
或
数字指定精度和范围从 0 到 3(默认值)。
您可以使用:
获得如下所示的输出:
l
(ell) 给出长格式。To use the Bash builtin
time
rather than/bin/time
you can set this variable:which will output the real time that looks like this:
or
The number specifies the precision and can range from 0 to 3 (the default).
You can use:
to get output that looks like:
The
l
(ell) gives a long format.来自 time 的手册页:
/usr/bin/time
您可以提供格式字符串,格式选项之一是经过的时间 - 例如
%E
/usr/bin/time -f'%E' $CMD
示例:
From the man page for time:
/usr/bin/time
You can provide a format string and one of the format options is elapsed time - e.g.
%E
/usr/bin/time -f'%E' $CMD
Example:
使用 bash 内置变量
SECONDS
。每次引用该变量时,它将返回自脚本调用以来经过的时间。示例:
输出:
Use the bash built-in variable
SECONDS
. Each time you reference the variable it will return the elapsed time since the script invocation.Example:
Output:
不太确定您在问什么,您是否尝试过:
编辑:好的,所以您知道如何消除超时,而您只想更改格式。如果您描述了所需的格式,将会有所帮助,但这里有一些可以尝试的事情:
这会将输出更改为每行一次(以秒为单位),带小数。您只需要实时,而不需要其他两个,因此要获取秒数,请使用:
Not quite sure what you are asking, have you tried:
Edit: ok, so you know how to get the times out and you just want to change the format. It would help if you described what format you want, but here are some things to try:
This changes the output to one time per line in seconds with decimals. You only want the real time, not the other two so to get the number of seconds use:
接受的答案给了我这个输出
这就是我解决问题的方法
The accepted answer gives me this output
This is how I solved the issue