Otra posibilidad, con algunos bash
sabores, es activar la opción pipefail
:
pipefail
Si establecer, el valor de retorno de una tubería es el valor de la última (más a la derecha) comm y para salir con un estado distinto de cero , o cero si todos los comandos en la tubería salen exitosamente. Esta opción está deshabilitada de forma predeterminada.
set -o pipefail
...
command 2>&1 | tee -a file.txt || echo "Command (or tee?) failed with status $?"
Dicho esto, la única manera de lograr la funcionalidad PIPESTATUS
forma portátil (por ejemplo, por lo que también trabajaría con POSIX sh
) es un poco complicado, es decir, se requiere un archivo temporal para propagar una estado de salida del tubo de nuevo al proceso shell padre:
{ command 2>&1 ; echo $? >"/tmp/~pipestatus.$$" ; } | tee -a file.txt
if [ "`cat \"/tmp/~pipestatus.$$\"`" -ne 0 ] ; then
...
fi
o, encapsulado para su reutilización:
log2file() {
LOGFILE="$1" ; shift
{ "[email protected]" 2>&1 ; echo $? >"/tmp/~pipestatus.$$" ; } | tee -a "$LOGFILE"
MYPIPESTATUS="`cat \"/tmp/~pipestatus.$$\"`"
rm -f "/tmp/~pipestatus.$$"
return $MYPIPESTATUS
}
log2file file.txt command param1 "param 2" || echo "Command failed with status $?"
o, más genéricamente quizá:
save_pipe_status() {
STATUS_ID="$1" ; shift
"[email protected]"
echo $? >"/tmp/~pipestatus.$$.$STATUS_ID"
}
get_pipe_status() {
STATUS_ID="$1" ; shift
return `cat "/tmp/~pipestatus.$$.$STATUS_ID"`
}
save_pipe_status my_command_id ./command param1 "param 2" | tee -a file.txt
get_pipe_status my_command_id || echo "Command failed with status $?"
...
rm -f "/tmp/~pipestatus.$$."* # do this in a trap handler, too, to be really clean
¿Por qué no quieres usar 'PIPESTATUS'? –
Duplicados: http://stackoverflow.com/questions/985876/tee-and-exit-status, http: // stackoverflow.com/questions/1221833/bash-tee-output-and-capture-exit-status – jpalecek