3

I have a bash script where I execute a command and tee to a file. On checking the return code, it is always 0 which is for the tee <> command.

make all | tee output.log
if [[ $? -ne 0 ]]; then
    echo "Make failed"
    exit 1
else
    blah blah
fi

Is there a way to check the return code of the first command (i.e. make all in this case)?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
oak1208
  • 303
  • 7
  • 16

2 Answers2

6

Suppose you have the commands pipe command1 | command2, you can get each command exit code by:

echo "${PIPESTATUS[0]} - ${PIPESTATUS[1]}"
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
3
if make all | tee output.log
then
  echo Could not write create output.log
  exit 2
elif (( ${PIPESTATUS[0]} > 0 ))
then
  echo Make failed
  exit 1
else
  echo Looks great
  ...
fi
user1934428
  • 19,864
  • 7
  • 42
  • 87