【问题标题】:pipe command to mailx and do not send mail if no content管道命令到 mailx,如果没有内容则不发送邮件
【发布时间】:2015-07-16 21:40:35
【问题描述】:

我的系统 (rhel5) 不支持 mailx-E 选项(如果正文为空则不发送电子邮件)。是否有一个我可以用来模拟这个功能的衬垫?例如,第一个会发送,但第二个不会

echo 'hello there' | blah | mailx -s 'test email' me@you.com
echo '' | blah | mailx -s 'test email' me@you.com

【问题讨论】:

    标签: bash pipe mailx


    【解决方案1】:

    嗯。 “单线”是相对而言的,因为这些在技术上是单线,但它们可能不适合您:

    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
    

    【讨论】:

    • 呵呵,是的...我知道你的意思。我有类似的东西,但我认为有一些内置的 bash 可以在某些条件下终止管道。
    • 是的,这绝对是您真正的问题。 This question here 似乎回答了这个问题。
    【解决方案2】:

    你可以用一个技巧而不是一个程序来尝试它:

    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
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 2014-05-27
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多