2

I'm running a script within a script, release.sh and caller.sh (caller.sh calls release.sh). Both are bash.

release.sh contains a bunch of conditions with 'exit 1' for errors.

caller.sh contains a line that goes "./release.sh", then a checks for the exit code of release.sh - if $? is greater than 0 then echo "release.sh screwed up" and exits.

./release.sh
        if [ $? -gt "0" ] ; then
            echo "release_manager exited with errors, please see the release log."
            exit 1
        else 
            echo "release manager is done." 
        fi

Recently I've decided to log release.sh, and so the line in caller.sh goes:

./release.sh 2>&1 | tee release.txt

Because of the pipe, $? is the exit code of 'tee release.txt' , which always comes out as 0, regardless of the previous command :-(

I have to check for release.sh errors and stop the caller from proceeding, but I also really need that log.

Is there a way to get the exit code of the command before last? Or a different way to log the release script in the same command?

I'm not really in charge of release.sh, I'd rather not change it.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Nahshon paz
  • 261
  • 5
  • 12

2 Answers2

2

Since you are using bash you can set in the script the option:

set -o pipefail

The pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

Alternatively, immediately after the piped command you can look at builtin variable value ${PIPESTATUS[0]} for the exit code of the first command in the pipe.

PIPESTATUS: An array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command).

meuh
  • 51,383
0

May I suggest rewriting it like that:

./release.sh > release.txt 2>&1

and then

echo $?

would return the correct exit status

man0v
  • 357