jeyGey
jeyGey

Reputation: 67

shell bash - return code

Is it possible to run a command to redirect output and standard error in a file and know the return code of the command ?

./COMMANDE.sh 2>&1 | tee -a $LOG_DIRECTORY/$LOG_FILE
if [ $? = 0 ]; then
    echo "OK"
else
    echo "KO"
    exit 1
fi

Currently, I get the return code of the tee command, I want to recover that of COMMANDE.sh.

Upvotes: 3

Views: 180

Answers (1)

DevSolar
DevSolar

Reputation: 70411

You want PIPESTATUS.

./COMMANDE.sh 2>&1 | tee -a $LOG_DIRECTORY/$LOG_FILE
if [ ${PIPESTATUS[0]} = 0 ]; then
    echo "OK"
else
    echo "KO"
    exit 1
fi

The index into the array ([0]) is the index of the command in the pipe chain just executed (./COMMANDE.sh). ${PIPESTATUS[1]} would be the tee.

Upvotes: 3

Related Questions