捕获的庆典时间脚本变量输出 [英] Capturing the output of bash time in script variable

查看:137
本文介绍了捕获的庆典时间脚本变量输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样的:

TIMEFORMAT=%R;
foo=$(time wget http://www.mysite.com)
echo $foo

和当我执行我看到我想要的输出,但人数不得变量foo的(回声$ foo的打印任何内容)。

and when I execute I see the number I want in the output but not in variable foo (echo $foo print nothing).

这是为什么?

推荐答案

您没有捕捉任何事情,因为时间将其输出的标准错误。麻烦的是,在的wget 命令也将其大部分的输出对标准错误。要分割的两个流(从 wget的扔掉输出),您将需要使用的子shell

You are not capturing anything in foo because time sends its output on stderr. The trouble is that the wget command also sends most of its output on stderr. To split the two streams (and throw away the output from wget) you will need to use a subshell:

TIMEFORMAT=%R;
foo=$( time ( wget http://www.example.com 2>/dev/null 1>&2 ) 2>&1 )
echo $foo

这是怎么回事...

Here is an explanation of what's going on...

此命令的内部:

( wget http://www.example.com 2>/dev/null 1>&2 )

同时发送标准错误标准输出的/ dev / null的,基本上是扔掉。

外部分:

foo=$( time ( ... ) 2>&1 )

发送标准错误时间命令相同的地方,标准输出发送,以便它可以由命令替换被捕获( $())。

Sends stderr from the time command to the same place that stdout is sent so that it may be captured by the command substitution ($()).

更新:

如果你想获得真正聪明的,你可以通过玩杂耍有的wget 穿过的输出标准错误该文件描述是这样的:

If you wanted to get really clever, you can have the output of wget passed through to stderr by juggling the file descriptors like this:

foo=$( time ( wget http://www.example.com 2>&1 ) 3>&1 1>&2 2>&3 )

这篇关于捕获的庆典时间脚本变量输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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