簡體   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