This question already has an answer here:
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.