时间命令的自定义格式 [英] Custom format for time command

查看:22
本文介绍了时间命令的自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 bash 脚本 中使用 time 命令来计算脚本的运行时间并将其写入日志文件.我只需要实时,而不是用户和系统.还需要它的体面格式.例如 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.

预期的格式应该是 00:00:00.0000(毫秒)[小时]:[分钟]:[秒].[毫秒]

The expected format supposed to be 00:00:00.0000 (milliseconds) [hours]:[minutes]:[seconds].[milliseconds]

我已经有 3 个脚本了.我看到一个这样的例子:

I've already 3 scripts. I saw an example like this:

{ time { # section code goes here } } 2> timing.log

但我只需要实时,不需要用户和系统.还需要它的体面格式.例如 00:00:00:00(不像标准输出).

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.

推荐答案

你可以使用 date 命令来获取执行要计时的工作前后的当前时间,并像这样计算差异:

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
" "$((T/86400))" "$((T/3600%24))" "$((T/60%60))" "$((T%60))""

注意事项:$((...)) 可用于 bash 中的基本算术 –注意:不要在减号 - 之前放置空格,因为这可能会被解释为命令行选项.

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.

另见:http://tldp.org/LDP/abs/html/arithexp.html


此外,您可能需要查看 sedtime 生成的输出中搜索和提取子字符串.


Additionally, you may want to take a look at sed to search and extract substrings from the output generated by time.

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

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
" "$((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.

这篇关于时间命令的自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆