开球和出口状态 [英] tee and exit status
问题描述
是否有tee"的替代方案,它捕获正在执行的命令的 STDOUT/STDERR,并以与处理命令相同的退出状态退出.如下:
is there an alternative to "tee" which captures STDOUT/STDERR of the command being executed and exits with the same exit status as the processed command. Something as following:
eet -a some.log -- mycommand --foo --bar
其中eet"是tee"的虚构替代品:)(-a 表示追加,-- 分隔捕获的命令)破解这样的命令应该不难,但也许它已经存在,而我不知道吗?
Where "eet" is an imaginary alternative to "tee" :) (-a means append, -- separates the captured command) It shouldn't be hard to hack such a command but maybe it already exists and I'm not aware of it?
谢谢.
推荐答案
这是一个 eet
.适用于我可以使用的所有 Bash,从 2.05b 到 4.0.
Here's an eet
. Works with every Bash I can get my hands on, from 2.05b to 4.0.
#!/bin/bash
tee_args=()
while [[ $# > 0 && $1 != -- ]]; do
tee_args=("${tee_args[@]}" "$1")
shift
done
shift
# now ${tee_args[*]} has the arguments before --,
# and $* has the arguments after --
# redirect standard out through a pipe to tee
exec | tee "${tee_args[@]}"
# do the *real* exec of the desired program
exec "$@"
(pipefail
和 $PIPESTATUS
很不错,但我记得它们是在 3.1 或大约版本中引入的.)
(pipefail
and $PIPESTATUS
are nice, but I recall them being introduced in 3.1 or thereabouts.)
这篇关于开球和出口状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!