繁体   English   中英

用管道命令发送到mailx,如果没有内容,则不发送邮件

[英]pipe command to mailx and do not send mail if no content

我有一个不支持mailx-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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM