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] )
Solution 4:
For some implementations of mail
the command line switch -E
is equivalent to --exec
which let's you execute a command. In this case daharon's answer works quite well. You can even shortened it to:
$ mail -E 'set nonullbody' -s Test [email protected]
If you want to test this behavior use the echo
command with the -n
command line switch to suppress the trailing newline.
This command sends an email:
$ echo -n "something" | mail -E 'set nonullbody' -s Test [email protected]
But this command doesn't send an email:
$ echo -n "" | mail -E 'set nonullbody' -s Test [email protected]