问题描述
我更新一个bash脚本将作为程序测试工具。
previously,我此行的脚本工作完美( $ BIN_FILE
设置为相对路径来测试的二进制文件):
I'm updating a bash script which serves as a program testing tool.Previously, I had this line in a script working perfectly ($BIN_FILE
is set to a relative path to the binary being tested):
$BIN_FILE $BIN_OPTS &> $LOG_FILE
然后我决定添加一些性能测试:
Then I've decided to add some "performance measurements":
time $BIN_FILE $BIN_OPTS &> $LOG_FILE"
这工作完全一样好,但同时运行多个测试时,脚本的输出是与所有这些真实的用户,系统太挤了。现在我传递第二个参数给脚本,这会导致$ TIME变量分配给它的价值时间。不过,
This worked perfectly as well, but when running many tests at once, script's output was too crowded with all those "real, user, system". Now I'm passing a second parameter to the script, which causes $TIME variable to have value 'time' assigned to it. However,
$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE
是行不通的。唯一的工作选择似乎
just doesn't work. The only working option seems to be
eval "$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE"
但它是丑陋的。
为什么不
$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE
工作?是否有一个更好的前瞻性解决方案,那么一个与评估的?
work? Is there a nicer-looking solution, then the one with eval?
此外,对于便携性 - 我应该使用bash内部的'时间',或者叫GNU的/ usr / bin / time会
Also, regarding portability - should I use bash's internal 'time', or call GNU /usr/bin/time?
推荐答案
时间
是一个保留在bash字。因为它是解析,因此之前的变量替换。例如,您不能使用内置的时间
是这样的:
time
is a reserved word in bash. It must be written explicitly because it is parsed before parsing the "simple commands", and therefore before variable substitutions. For example, you cannot use builtin time
in this way:
cat long-file | time sort
它必须是:
time cat long-file | sort
在这里庆典
将测量整个管道花费的时间。
and here bash
will measure time spend by whole pipeline.
GNU时间
是一个简单的二进制,所以它可以在方式使用你的意思是它...它可以在管道中间使用。但它并不总是装,所以我想你最好使用内建的。
GNU time
is a simple binary, so it can be used in the way you mean it... and it can be used in the middle of a pipeline. But it is not always installed, so I guess you'd better use builtin.
我猜你想忽略时间
在某些特定的情况下,因为这样的话你想要的变量替换。如果是这样,你可以用 GNU时间去
,评估
或某种如果..网络
结构,但你绝对不能使用内置你想要的方式。
I guess you want to omit time
in some specific cases and because of that you want variable substitution. If so, you can go with GNU time
, eval
or some kind of if..fi
structure, but you definitely cannot use builtin the way you wanted.
如果你想成为更便携,你应该使用它的 -p
选项来获得不同的实现相同的输出。见F.E. OpenGroup的的时间
描述。然后,无论您选择的实现,它的行为应该以同样的方式(只要是符合POSIX标准)。
If you want to be more portable, you should use its -p
option to get the same output under different implementations. See f.e. OpenGroup's time
description. Then whatever implementation you choose, it should behave the same way (as long as it is POSIX-compliant).
这篇关于我如何简化庆典的“EVAL" $ TIME $ $ BIN_FILE&BIN_OPTS放大器;> $ LOG_FILE"'并保持它的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!