如何简化 bash 的“eval”$TIME $BIN_FILE $BIN_OPTS > $LOG_FILE””并让它继续工作?

发布于 2024-08-12 06:13:15 字数 748 浏览 3 评论 0原文

我正在更新一个 bash 脚本,它用作程序测试工具。 以前,我在脚本中将此行完美地工作($BIN_FILE 设置为正在测试的二进制文件的相对路径):

$BIN_FILE $BIN_OPTS &> $LOG_FILE

然后我决定添加一些“性能测量”:

time $BIN_FILE $BIN_OPTS &> $LOG_FILE"

这完美地工作同样,但是当同时运行多个测试时,脚本的输出过于拥挤,充满了所有这些“真实的、用户的、系统的”。现在我将第二个参数传递给脚本,这会导致 $TIME 变量分配有值“time”。然而,

$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE

就是行不通。唯一可行的选择似乎是

eval "$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE"

但它很丑陋。

为什么不起作用

$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE

?有没有一种比带有eval的解决方案更好看的解决方案?

另外,关于可移植性 - 我应该使用 bash 的内部“时间”,还是调用 GNU /usr/bin/time?

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"

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"

but it's ugly.

Why doesn't

$TIME $BIN_FILE $BIN_OPTS &> $LOG_FILE

work? Is there a nicer-looking solution, then the one with eval?

Also, regarding portability - should I use bash's internal 'time', or call GNU /usr/bin/time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-08-19 06:13:15

time 是 bash 中的保留字。它必须显式编写,因为它是在解析“simple 之前解析的命令”,因此在变量替换之前。例如,您不能以这种方式使用内置 time

cat long-file | time sort

它必须是:

time cat long-file | sort

这里 bash 将测量整个管道花费的时间。

GNU time 是一个简单的二进制文件,因此可以按照您的意思使用...并且可以在管道中间使用。但它并不总是安装的,所以我想你最好使用内置的。

我猜你想在某些特定情况下省略 time ,因此你需要变量替换。如果是这样,你可以使用GNU timeeval或某种if..fi结构,但你绝对不能使用内置的方式你想要的。

如果你想更加可移植,你应该使用它的 -p 选项在不同的实现下获得相同的输出。请参阅 OpenGroup 的时间说明。那么无论您选择什么实现,它的行为方式都应该相同(只要它符合 POSIX 标准)。

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

It has to be:

time cat long-file | sort

and here bash will measure time spend by whole pipeline.

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.

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.

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).

嘦怹 2024-08-19 06:13:15

您可能会发现对 time 的输出使用较短的格式很有帮助:

$ /usr/bin/time -f "Real %E" sleep 1
Real 0:01.02
$ TIMEFORMAT="Real %R"
$ time sleep 1        # Bash builtin
Real 1.027

任一版本的 time 的输出都可以进一步自定义(对于 Bash 内置程序来说则不然)。

You may find it helpful to use a shorter format for the output of time:

$ /usr/bin/time -f "Real %E" sleep 1
Real 0:01.02
$ TIMEFORMAT="Real %R"
$ time sleep 1        # Bash builtin
Real 1.027

The output of either version of time can be further customized (less so for the Bash builtin).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文