Can't figure out how to send ^D (EOT) signal t

2019-05-26 16:10发布

问题:

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