The following script runs on Solaris using /bin/ksh and on Linux using /bin/sh
cmd | tee -a cmd.log | tail
exit $?
The output of cmd is saved in a file cmd.log and the last lines are displayed on stdout
.
The purpose of exit $?
was to exit the script with the return code of cmd. Of course this
does not work because $?
holds the return code of the last command in the pipeline which is
tail
.
Workaround (I will omit all cleanup activities) :
{ cmd; echo $? > error.file; } | tee -a cmd.log | tail
exit `cat error.file`
But is there another way to get the returncode of cmd and to avoid the creation of a file like error.file?
set -o pipefail
works in bash and ksh. – Kevin Oct 16 '13 at 15:17