KornShell equivalant of PIPESTATUS

会有一股神秘感。 提交于 2019-12-10 11:53:14

问题


. ${script_name} | tee -a ${log_file}

KornShell unfortunately does not have a PIPESTATUS command like Bash does and I would like to know if anybody has an elegant solution to getting the exit status of the first command (above). This is what I've pieced together from code found around the internets.

{
typeset -r script_rc=$(
{
  {
    . ${script_name}
    echo "$?" >&3
  } | tee -a ${log_file}
} 3>&1 >&4 4>&-
)
} 4>&1

Unfortunately this code is hard to read and I was wondering if someone knew of something more readable.


回答1:


You can use process substitution to avoid the pipe altogether, just like in bash

. "${script_name}" > >(tee -a "${log_file}")

This has the added advantage of actually running $script_name in the current shell, which I assume is the point of using . to run it. $? will not be affected by the exit status of the process substitution.




回答2:


Try turning on pipefail

set -o pipefail

This will give return the first non-zero return code within a pipe of parts.

Not as robust as PIPESTATUS. Makes debugging a little more hands on. But you'll a least capture a failing part of a pipe without an error return code being swallowed and pressing on.



来源:https://stackoverflow.com/questions/35463581/kornshell-equivalant-of-pipestatus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!