Quote:
Originally Posted by pankai
I have a question about how to get the exit code of the first command when it appears in a pipe-lined command.
For example, I have the following script:
grep abc dddd | tee -a log
if [[ $? -ne 0 ]]
then
echo "ERROR!"
fi
In the above script, [[ $? -ne 0 ]] is supposed to test the exit code of "grep abc dddd". But since it is in a pipe line, the $? actually stores the exit code of "tee -a log". So, [[ $? -ne 0 ]] is always false.
How to address this problem?
Thanks.
|
I did some research myself. I turned out that we can get the status code for each command in a pipeline from $PIPESTATUS array.