Can't figure out how to send ^D (EOT) signal to mailx in bash script

青春壹個敷衍的年華 提交于 2019-12-22 11:35:42

问题


I'm writing a bash script to send me an email automatically. Mailx requires an EOT or ^D signal to know the message body is over and it can send. I don't want to hit ^D on the keyboard when I run script which is what it does now.

Here is my code:

#! /bin/bash
SUBJ="Testing"
TO="test@test.com"
MSG="message.txt"

echo "I am emailing you" >> $MSG
echo "Time: `date` " >> $MSG

mail -s "$SUBJ" -q "$MSG" "$TO"

rm -f message.txt

回答1:


If you do not need to add more text and just need to send the content of $MSG, you can replace

mail -s "$SUBJ" -q "$MSG" "$TO"

with

mail -s "$SUBJ" "$TO" < "$MSG"

The EOT will be implicit in the < construct. -q is indeed only used to start a message. The rest is supposed to come through stdin.




回答2:


Pipe the output of a command group to mail.

#! /bin/bash
SUBJ="Testing"
TO="test@test.com"
MSG="message.txt"

{
  echo "I am emailing you"
  echo "Time: `date` "
} | mail -s "$SUBJ" -q "$MGS" "$TO"

rm -f message.txt


来源:https://stackoverflow.com/questions/19798650/cant-figure-out-how-to-send-d-eot-signal-to-mailx-in-bash-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!