奋力解析(bash)的时间命令 [英] Struggling to parse (bash) time command

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

问题描述

我竭力要解析的时候命令的输出在bash - 甚至从打印出它的输出,当我把它停下来。这是我的测试code:

I'm struggling to parse the output of the time command in bash - and even to stop it from printing out its output when I call it. This is my test code:

#!/bin/bash
TIME=`time ls -lh > /dev/null`
echo "Testing..."
echo $TIME

这目​​前打印出:

{blank-line}
real    0m0.064s
user    0m0.002s
sys     0m0.005s
Testing
{blank-line}

所以,好像分配到 $ TIME 的值是当时打印出来开始的空行。我需要在SYS行秒钟值 - 也就是说,0.005。我保证我仅会秒钟,所以我不米之前需要什么 - 但是,部分秒钟可能是xx.xxx的形式,如果去> = 10秒。我现在不知道如何用晚餐preSS的'时间'输出,捕捉到它,而不是所有的空行,也不解析它来获得我所需要的值。

So, it seems like the value assigned to $TIME is the blank line at the start of the time print-out. I need to get at the seconds value of the sys line - that is, the "0.005". I am guaranteed that I will only ever have seconds, so I do not need anything before the "m" - however, the seconds part may be in the form of xx.xxx if it goes >= 10 seconds. I currently have no idea how to suppress the 'time' output, capture it all instead of the blank line, nor parse it to get the values I need.

任何帮助将是非常美联社preciated ...

Any help would be much appreciated...

推荐答案

如果您使用的Bash内建时间,你可以通过设置<$ C $控制其输出C> TIMEFORMAT 变量:

If you use the Bash builtin time, you can control its output by setting the TIMEFORMAT variable:

TIMEFORMAT=%R

和你将不必做任何分析,因为这将导致时间只输出的秒数。

and you won't have to do any parsing since that will cause time to only output the number of seconds.

和使用:

echo "Testing..."
TIME=$( { time ls -lh > /dev/null; } 2>&1 )
echo $TIME

BashFAQ / 032

这篇关于奋力解析(bash)的时间命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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