I’ve got a shell script where I’d like to know when an error happens. Typically when that happens, something gets written to stdout or stderr - and I’d like to see that. But when things are just peachy, I don’t want to be bothered.
Here’s an easy way to achieve that. At the beginning of my script, I have:
WEATHEROUT=`/bin/mktemp`
WEATHERERR=`/bin/mktemp`
(Did I mention this is a script for a weather station? Yep.)
Then in the body of the script, I have:
/usr/local/bin/do-the-thing > ${WEATHEROUT} 2> ${WEATHERERR}
/usr/local/bin/do-the-other-thing >> ${WEATHEROUT} 2>> ${WEATHERERR}
Finally, at the end of the script, there’s:
if [ -s ${WEATHERERR} -o -s ${WEATHEROUT} ]; then
cat ${WEATHEROUT} ${WEATHERERR} | /usr/bin/mail -s "Weather command error" me@myaddr
fi
That’s all!