. ${script_name} | tee -a ${log_file}
不幸的是,KornShell 没有像 Bash 那样的 PIPESTATUS 命令,我想知道是否有人有一个优雅的解决方案来获取第一个命令的退出状态(上面)。这是我从互联网上找到的代码拼凑而成的。
{
typeset -r script_rc=$(
{
{
. ${script_name}
echo "$?" >&3
} | tee -a ${log_file}
} 3>&1 >&4 4>&-
)
} 4>&1
不幸的是,这段代码很难阅读,我想知道是否有人知道更具可读性的东西。
最佳答案
您可以使用进程替换来完全避免管道,就像在 bash
. "${script_name}" > >(tee -a "${log_file}")
这具有在当前 shell 中实际运行 $script_name
的额外优势,我认为这就是使用 .
运行它的要点。 $?
不会受到进程替换退出状态的影响。
关于bash - Korn Shell 相当于 PIPESTATUS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35463581/