我有一个不支持mailx
's-E
选项的系统(rhel5)(如果正文为空,则不发送电子邮件)。是否有一个我可以用来模拟这个功能的衬垫?例如,第一个会发送,但第二个不会
echo 'hello there' | blah | mailx -s 'test email' me@you.com
echo '' | blah | mailx -s 'test email' me@you.com
您可以使用技巧而不是通过管道传输到的程序来尝试它:
msg='hello there' && [ -n "$msg" ] && echo "$msg" | mailx -s 'test email' me@you.com
如果您的消息来自另一个脚本,则必须将其运行为
msg="$(get_it)" && [ -n "$msg" ] && echo "$msg" | mailx -s 'test email' me@you.com
如果[ ... ]
不支持,您也可以使用[[ ... ]]
:
msg="$(get_it)" && [[ -n "$msg" ]] && echo "$msg" | mailx -s 'test email' me@you.com
出色地。“单线”是相对的,因为这些在技术上是单线,但它们可能不适合您:
stuff=$(echo 'hello there') ; [ -n "${stuff}" ] && echo ${stuff} | mailx -s 'test email' me@you.com
stuff=$(echo '') ; [ -n "${stuff}" ] && echo ${stuff} | mailx -s 'test email' me@you.com