tee and exit status tee and exit status unix unix

tee and exit status


This works with bash:

(  set -o pipefail  mycommand --foo --bar | tee some.log)

The parentheses are there to limit the effect of pipefail to just the one command.

From the bash(1) man page:

The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, 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.


Stumbled upon a couple of interesting solutions here http://www.perlmonks.org/?node_id=597613.

1) There is $PIPESTATUS variable available in bash:

   false | tee /dev/null   [ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS

2) And the simplest prototype of "eet" in perl may look as follows:

   open MAKE, "command 2>&1 |" or die;   open (LOGFILE, ">>some.log") or die;   while (<MAKE>) { print LOGFILE $_; print }   close MAKE; # to get $?   my $exit = $? >> 8;   close LOGFILE;


Here's an eet. Works with every Bash I can get my hands on, from 2.05b to 4.0.

#!/bin/bashtee_args=()while [[ $# > 0 && $1 != -- ]]; do    tee_args=("${tee_args[@]}" "$1")    shiftdoneshift# now ${tee_args[*]} has the arguments before --,# and $* has the arguments after --# redirect standard out through a pipe to teeexec | tee "${tee_args[@]}"# do the *real* exec of the desired programexec "$@"

(pipefail and $PIPESTATUS are nice, but I recall them being introduced in 3.1 or thereabouts.)