2
. ${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.

Jacek Trociński
  • 882
  • 1
  • 8
  • 23

2 Answers2

2

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Nice! You can add examples: `rm nonexistfile 2>&1 > >(tee /dev/null); echo $?` and repeat after `touch nonexistfile`. – Walter A Feb 17 '16 at 22:34
1

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.

hrobertv
  • 158
  • 1
  • 8