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

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

问题描述

我正在从Bash脚本C程序,并通过调用时命令,该命令输出的算法的运行了一段时间的统计数据运行它。

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

取决于算法的运行值

The values depending on the running of the algorithm

不过,我希望能够做的是采取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?

非常感谢

推荐答案

您一定要小心:有内置猛砸时间并有外部命令时间,通常位于的/ usr / bin / time会(类型为键入-a时间让所有可用的时间是你的系统)。

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

你调用内置的时间。您不能直接抓到的时间输出而不一些小的挂羊头卖狗肉。这是因为时间不希望与可能重定向或管道,你会进行干涉,这是一件好事。

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.

要获得标准输出,你需要时间输出:

To get time output on standard out, you need:

{ time stuff; } 2>&1

(分组和重定向)。

(grouping and redirection).

现在,大约解析输出:解析命令的输出通常是一个坏主意,特别是当它可能没有做。幸运的是,猛砸的时间命令接受一个格式字符串。从手册:

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

这个参数的值用作格式字符串指定如何pfixed回馈字应被显示的时间为管道$ P $定时信息。在字符引入了被扩展为时间值或其他信息转义序列。是转义序列和它们的含义如下:括号表示可选部分。

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] [1] R

   The elapsed time in seconds.


  
  

%[P] [1] U

   The number of CPU seconds spent in user mode.


  
  

%[P] [1] S

   The number of CPU seconds spent in system mode.


  
  

%P

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


  
  

可选的 P 是一个数字指定precision,小数位小数点后的位数。值0将导致没有小数点或分数输出。小数点后最多三个地方可以规定; p大于3的值更改为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.

可选的指定格式为: 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

$'\\ nreal \\ T%3LR \\ n用户\\ T%3lU \\ NSYS \\ T%3LS

如果值为null,则不显示时序信息。显示格式字符串时一个尾随的换行符被添加。

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 上精彩的格雷格的维基

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

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