Search code examples
bashpipemailx

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


I have a system (rhel5) that does not support mailx's -E option (for not sending email if the body is empty). Is there a one liner that i could use to simulate this feature? eg the first would send, but the second wouldn't

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

Solution

  • Well. "one-liner" is sort of relative, since these are technically one-liners but they may not suit you:

    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