Un'altra possibilità, con alcuni bash
sapori, è quello di attivare l'opzione pipefail
:
pipefail
Se impostato, il valore restituito di una pipeline è il valore dell'ultimo (più a destra) comm e per uscire con uno stato diverso da zero, oppure zero se tutti i comandi nella pipeline terminano correttamente. Questa opzione è disabilitata per impostazione predefinita.
set -o pipefail
...
command 2>&1 | tee -a file.txt || echo "Command (or tee?) failed with status $?"
Ciò detto, l'unico modo per raggiungere PIPESTATUS
funzionalità portabile (es quindi sarebbe anche funzionare con POSIX sh
) è un po 'complicata, cioè richiede un file temporaneo propagare una stato di uscita del tubo di nuovo al processo shell genitore:
{ command 2>&1 ; echo $? >"/tmp/~pipestatus.$$" ; } | tee -a file.txt
if [ "`cat \"/tmp/~pipestatus.$$\"`" -ne 0 ] ; then
...
fi
o, incapsulando reimpiegate:
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, più genericamente forse:
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
Perché non si desidera utilizzare 'PIPESTATUS'? –
Duplicati: http://stackoverflow.com/questions/985876/tee-and-exit-status, http: // stackoverflow.com/domande/1221833/bash-tee-uscita-e-cattura-exit-status – jpalecek