0

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
yee379
  • 6,498
  • 10
  • 56
  • 101

2 Answers2

0

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
Karsten S.
  • 2,349
  • 16
  • 32
0

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
pooley1994
  • 723
  • 4
  • 16
  • hehe, yeah... i know what you mean. i had something similar but i was thinking that there was some bash built-in that could terminate the pipe upon some condition. – yee379 Jul 16 '15 at 23:19
  • Yeah, that definitely is your real question. [This question here](http://stackoverflow.com/questions/8976139/shell-pipe-exit-immediately-when-one-command-fails) seems to answer that. – pooley1994 Jul 17 '15 at 15:04