无法弄清楚如何在 bash 脚本中向 mailx 发送 ^D (EOT) 信号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19798650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:30:44  来源:igfitidea点击:

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

bashemailmailxeot

提问by GuavaKhan

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.

我正在编写一个 bash 脚本来自动向我发送电子邮件。Mailx 需要 EOT 或 ^D 信号才能知道邮件正文已结束并且可以发送。我不想在运行脚本时敲击键盘上的 ^D,这就是它现在所做的。

Here is my code:

这是我的代码:

#! /bin/bash
SUBJ="Testing"
TO="[email protected]"
MSG="message.txt"

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

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

rm -f message.txt

回答by damienfrancois

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

如果不需要添加更多的文字,只需要发送$MSG的内容,可以替换

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

with

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

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

EOT会在隐<结构。-q确实只用于开始消息。其余的应该通过标准输入。

回答by chepner

Pipe the output of a command group to mail.

将命令组的输出通过管道传输到mail.

#! /bin/bash
SUBJ="Testing"
TO="[email protected]"
MSG="message.txt"

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

rm -f message.txt