时间命令的自定义格式

发布于 2024-09-19 03:24:15 字数 453 浏览 6 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空‖城人不在 2024-09-26 03:24:15

您可以使用date命令获取执行要计时的工作之前和之后的当前时间,并计算差值,如下

#!/bin/bash

# Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
T="$(date +%s)"

# Do some work here
sleep 2

T="$(($(date +%s)-T))"
echo "Time in seconds: ${T}"

printf "Pretty format: %02d:%02d:%02d:%02d\n" "$((T/86400))" "$((T/3600%24))" "$((T/60%60))" "$((T%60))""

所示:
$((...)) 可用于 bash 中的基本算术 - 注意:不要在减号 - 之前添加空格,因为这样可能被解释为命令行选项。

另请参阅:http://tldp.org/LDP/abs/html/arithexp.html< /a>

编辑:
此外,您可能还需要查看sed< /a> 从 time 生成的输出中搜索并提取子字符串。

编辑:

以毫秒计时的示例(实际上是纳秒,但此处截断为毫秒)。您的 date 版本必须支持 %N 格式,并且 bash 应支持大数字。

# UNIX timestamp concatenated with nanoseconds
T="$(date +%s%N)"

# Do some work here
sleep 2

# Time interval in nanoseconds
T="$(($(date +%s%N)-T))"
# Seconds
S="$((T/1000000000))"
# Milliseconds
M="$((T/1000000))"

echo "Time in nanoseconds: ${T}"
printf "Pretty format: %02d:%02d:%02d:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}"

免责声明:
我的原始版本说,

M="$((T%1000000000/1000000))"

但这已被删除,因为它显然对某些人不起作用,而据报道新版本却有效。我不同意这一点,因为我认为你必须只使用剩余的部分,但被否决了。
选择适合你的。

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:

#!/bin/bash

# Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
T="$(date +%s)"

# Do some work here
sleep 2

T="$(($(date +%s)-T))"
echo "Time in seconds: ${T}"

printf "Pretty format: %02d:%02d:%02d:%02d\n" "$((T/86400))" "$((T/3600%24))" "$((T/60%60))" "$((T%60))""

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 and bash should support large numbers.

# UNIX timestamp concatenated with nanoseconds
T="$(date +%s%N)"

# Do some work here
sleep 2

# Time interval in nanoseconds
T="$(($(date +%s%N)-T))"
# Seconds
S="$((T/1000000000))"
# Milliseconds
M="$((T/1000000))"

echo "Time in nanoseconds: ${T}"
printf "Pretty format: %02d:%02d:%02d:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}"

DISCLAIMER:
My original version said

M="$((T%1000000000/1000000))"

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.

瑾夏年华 2024-09-26 03:24:15

要使用 Bash 内置 time 而不是 /bin/time,您可以设置此变量:

TIMEFORMAT='%3R'

它将输出如下所示的实时时间:

5.009

65.233

数字指定精度和范围从 0 到 3(默认值)。

您可以使用:

TIMEFORMAT='%3lR'

获得如下所示的输出:

3m10.022s

l (ell) 给出长格式。

To use the Bash builtin time rather than /bin/time you can set this variable:

TIMEFORMAT='%3R'

which will output the real time that looks like this:

5.009

or

65.233

The number specifies the precision and can range from 0 to 3 (the default).

You can use:

TIMEFORMAT='%3lR'

to get output that looks like:

3m10.022s

The l (ell) gives a long format.

萤火眠眠 2024-09-26 03:24:15

来自 time 的手册页

  1. 可能有一个名为 time 的内置 shell,请避免通过指定 /usr/bin/time
  2. 您可以提供格式字符串,格式选项之一是经过的时间 - 例如 %E

    /usr/bin/time -f'%E' $CMD

示例:

$ /usr/bin/time -f'%E' ls /tmp/mako/
res.py  res.pyc
0:00.01

From the man page for time:

  1. There may be a shell built-in called time, avoid this by specifying /usr/bin/time
  2. 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:

$ /usr/bin/time -f'%E' ls /tmp/mako/
res.py  res.pyc
0:00.01
柏拉图鍀咏恒 2024-09-26 03:24:15

使用 bash 内置变量 SECONDS。每次引用该变量时,它将返回自脚本调用以来经过的时间。

示例:

echo "Start $SECONDS"
sleep 10
echo "Middle $SECONDS"
sleep 10
echo "End $SECONDS"

输出:

Start 0
Middle 10
End 20

Use the bash built-in variable SECONDS. Each time you reference the variable it will return the elapsed time since the script invocation.

Example:

echo "Start $SECONDS"
sleep 10
echo "Middle $SECONDS"
sleep 10
echo "End $SECONDS"

Output:

Start 0
Middle 10
End 20
菩提树下叶撕阳。 2024-09-26 03:24:15

不太确定您在问什么,您是否尝试过:

time yourscript | tail -n1 >log

编辑:好的,所以您知道如何消除超时,而您只想更改格式。如果您描述了所需的格式,将会有所帮助,但这里有一些可以尝试的事情:

time -p script

这会将输出更改为每行一次(以秒为单位),带小数。您只需要实时,而不需要其他两个,因此要获取秒数,请使用:

time -p script | tail -n 3 | head -n 1

Not quite sure what you are asking, have you tried:

time yourscript | tail -n1 >log

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:

time -p script

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:

time -p script | tail -n 3 | head -n 1
岁月静好 2024-09-26 03:24:15

接受的答案给了我这个输出

# bash date.sh
Time in seconds: 51
date.sh: line 12: unexpected EOF while looking for matching `"'
date.sh: line 21: syntax error: unexpected end of file

这就是我解决问题的方法

#!/bin/bash

date1=$(date --date 'now' +%s) #date since epoch in seconds at the start of script
somecommand
date2=$(date --date 'now' +%s) #date since epoch in seconds at the end of script
difference=$(echo "$((date2-$date1))") # difference between two values
date3=$(echo "scale=2 ; $difference/3600" | bc) # difference/3600 = seconds in hours
echo SCRIPT TOOK $date3 HRS TO COMPLETE # 3rd variable for a pretty output.

The accepted answer gives me this output

# bash date.sh
Time in seconds: 51
date.sh: line 12: unexpected EOF while looking for matching `"'
date.sh: line 21: syntax error: unexpected end of file

This is how I solved the issue

#!/bin/bash

date1=$(date --date 'now' +%s) #date since epoch in seconds at the start of script
somecommand
date2=$(date --date 'now' +%s) #date since epoch in seconds at the end of script
difference=$(echo "$((date2-$date1))") # difference between two values
date3=$(echo "scale=2 ; $difference/3600" | bc) # difference/3600 = seconds in hours
echo SCRIPT TOOK $date3 HRS TO COMPLETE # 3rd variable for a pretty output.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文