suppress blank emails

Solution 1:

#!/bin/sh  

string=`some command`
len=${#string} 
if [ "$len" -gt "0" ]
   then echo $string | mail -s "tables altered on `hostname`" [email protected]
fi  

This will send the mail only if the output of the command is at least 1 character long, but this may include blanks etc.

(The solution above works, but is unnecessary. man mail reveals the -E option):

some command | mail -E -s "tables altered on `hostname`" [email protected]

Solution 2:

I use the following:

$ mail --exec 'set nonullbody' -s Test [email protected]

Which I found in the GNU mail documentation under the nullbody section.


Solution 3:

One-liner version of SvenW's answer (the creds should go to him, not me)

string=`some command`; [ "$len" -gt "0" ] && ( echo $string | mail -s "tables altered on `hostname`" [email protected] )