简体   繁体   中英

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

You can try it with a trick rather than a program to pipe to:

msg='hello there' && [ -n "$msg" ] && echo "$msg" | mailx -s 'test email' me@you.com

If your message comes from another script you would have to run it as

msg="$(get_it)" && [ -n "$msg" ] && echo "$msg" | mailx -s 'test email' me@you.com

If [ ... ] is not supported you can also use [[ ... ]] :

msg="$(get_it)" && [[ -n "$msg" ]] && echo "$msg" | mailx -s 'test email' me@you.com

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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