问题描述
我想这样的:
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的扔掉输出
),您将需要使用的:
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 )
这篇关于捕获的庆典时间脚本变量输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!