问题
. ${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