解析 Bash 内置时间的输出 [英] Parsing the output of Bash's time builtin

查看:25
本文介绍了解析 Bash 内置时间的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Bash 脚本运行 C 程序,并通过名为 time 的命令运行它,该命令会输出一些运行算法的时间统计信息.

I'm running a C program from a Bash script, and running it through a command called time, which outputs some time statistics for the running of the algorithm.

如果我要执行命令

time $ALGORITHM $VALUE $FILENAME

它产生输出:

real    0m0.435s
user    0m0.430s
sys     0m0.003s

取决于算法运行的值

但是,我希望能够做的是取 0.435 并将其分配给一个变量.我已经读过一些 awk,足以知道如果我将上述命令通过管道传输到 awk,我应该能够获取 0.435 并将其放入一个变量中.但我该怎么做?

However, what I would like to be able to do is to take the 0.435 and assign it to a variable. I've read into awk a bit, enough to know that if I pipe the above command into awk, I should be able to grab the 0.435 and place it in a variable. But how do I do that?

非常感谢

推荐答案

你必须小心:Bash 内置 time 和外部命令 time,通常位于在 /usr/bin/time 中(输入 type -a time 以获得系统上所有可用的 time ).

You must be careful: there's the Bash builtin time and there's the external command time, usually located in /usr/bin/time (type type -a time to have all the available times on your system).

如果你的shell是Bash,当你发出

If your shell is Bash, when you issue

time stuff

您正在调用内置的time.如果没有一些小技巧,您无法直接捕获 time 的输出.这是因为 time 不想干扰您将执行的可能的重定向或管道,这是一件好事.

you're calling the builtin time. You can't directly catch the output of time without some minor trickery. This is because time doesn't want to interfere with possible redirections or pipes you'll perform, and that's a good thing.

要在标准输出上获得 time 输出,您需要:

To get time output on standard out, you need:

{ time stuff; } 2>&1

(分组和重定向).

现在,关于解析输出:解析命令的输出通常是一个坏主意,尤其是在可以不用的情况下.幸运的是,Bash 的 time 命令接受格式字符串.来自手册:

Now, about parsing the output: parsing the output of a command is usually a bad idea, especially when it's possible to do without. Fortunately, Bash's time command accepts a format string. From the manual:

TIMEFORMAT

此参数的值用作格式字符串,指定应如何显示以时间保留字为前缀的管道的计时信息.% 字符引入了一个扩展为时间值或其他信息的转义序列.转义序列及其含义如下;大括号表示可选部分.

The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

%%

   A literal `%`.

%[p][l]R

   The elapsed time in seconds.

%[p][l]U

   The number of CPU seconds spent in user mode.

%[p][l]S

   The number of CPU seconds spent in system mode.

%P

   The CPU percentage, computed as (%U + %S) / %R. 

可选的p 是指定精度的数字,小数点后的小数位数.值为 0 会导致不输出小数点或分数.最多可指定小数点后三位;大于 3 的 p 值更改为 3.如果未指定 p,则使用值 3.

The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p is not specified, the value 3 is used.

可选的 l 指定更长的格式,包括分钟,格式为 MMmSS.FFs.p 的值决定了是否包含分数.

The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.

如果这个变量没有被设置,Bash 就像它有这个值一样

If this variable is not set, Bash acts as if it had the value

$' real %3lR user %3lU sys %3lS'

$' real %3lR user %3lU sys %3lS'

如果值为空,则不显示计时信息.显示格式字符串时添加尾随换行符.

If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.

因此,要完全实现您想要的:

So, to fully achieve what you want:

var=$(TIMEFORMAT='%R'; { time $ALGORITHM $VALUE $FILENAME; } 2>&1)

正如 @glennjackman 指出的那样,如果您的命令向标准输出和标准错误发送任何消息,您必须也要注意这一点.为此,需要一些额外的管道:

As @glennjackman points out, if your command sends any messages to standard output and standard error, you must take care of that too. For that, some extra plumbing is necessary:

exec 3>&1 4>&2
var=$(TIMEFORMAT='%R'; { time $ALGORITHM $VALUE $FILENAME 1>&3 2>&4; } 2>&1)
exec 3>&- 4>&-

来源:BashFAQ032关于精彩的Greg 的 wiki.

这篇关于解析 Bash 内置时间的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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