禁止空白的电子邮件

如何禁止空白电子邮件? 例如,在下面的例子中,我会喜欢

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

发送消息后,我收到以下消息:

 Null message body; hope that's ok 

这不好。 如果没有文字,我不想发送邮件。

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

只有当命令的输出长度至less为1个字符时,才会发送邮件,但这可能包含空格等。

(上面的解决scheme工作,但没有必要, man mail显示-E选项):

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

我使用以下内容:

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

我在GNU邮件文档中发现了nullbody部分。

SvenW的答案的单线版本(信任应该去他,而不是我)

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

例:

回声-n“”| 邮件-E -s“日志消息”[email protected]

说明:

一个空string有结尾字符,所以我们必须添加-n来排除这个testingecho \ n(cr)。 但是如果你的身体信息不存在,邮件将退出/停止发送。

来源:

为什么邮件-E让我发送空邮件? http://heirloom.sourceforge.net/mailx/mailx.1.html