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.
You can check the size of the logfile before and after you grep for the string. This way if the size of the logfile remains the same then you know the grep failed.