I want to be able to do the following:
command 2>&1 | shell_script.sh "subject line"
Where the stdout and sterr of running command will be piped to shell_script.sh for sending as the body of an email. The thing that I want to get is the exit status of command 2>&1 so that I can append the proper job status to the email subject. With the current implementation, when command 2>&1 is piped, the ${PIPESTATUS[0]} is always set to 0. If I remove the 2>&1, however, then the stderr output doesn't get piped into the email body. Been playing with several different variations but still can't figure it out.
The following is my shell_script.sh:
#!/usr/bin/env bash
SUBJ="$@"
read EMAIL_BODY
jobsuccess=${PIPESTATUS[0]}
if [ $jobsuccess -eq 0 ] # Job succeeded
then
echo $EMAIL_BODY | mail -s "$SUBJ Success" [email protected]
else
echo $EMAIL_BODY | mail -s "$SUBJ Fail" [email protected]
fi