I have the following script as shown below where I cat a file and then also tee the output to a file as I have to email the execution of the process to users at the end of the script:
cat incoming.dat | tee -a execution.log
if [ $? != 0 ]
then
echo "Issue with incoming.dat file, file not present"
exit 1
else
echo "data from incoming.dat file as follows:"
fi
echo -e "Execution Log" | mail -s "Log File" -a execution.log info@abc.com
The problem with above command is when there is no incoming.dat file present, the script isnt failing. When incoming.dat file isnt present, the cat command fails but since it able to successfully direct the output to tee, it never goes to "exit 1" part of the if statement. I cant remove the tee part as I need to print the command output in the stdout as well as direct it to a file as I have to email it to users. How can I fix this issue?
Thanks for any inputs,
Carl
Sorry, I wasn't clear. My issue is more to do with using the "tee" command which is causing the error checking (in the next if statement) to always pass regardless of the actual command whether it runs successfully or fails.
I understood your dilemma. That's why I gave that snippet. It is not only tee but for any chained commands: $? will report about the last command executed.
Is the following not your intention?
if [[ -s incoming.dat ]]; then
echo "data from incoming.dat file as follows:"
cat incoming.dat | tee -a execution.log
echo "Execution Log" | mail -s "Log File" -a execution.log info@abc.com
else
echo "Issue with incoming.dat: file not present or empty"
exit 1
fi