使用unix shell脚本发送电子邮件

kk.*_*kk. 8 unix email bash shell scripting

我必须编写一个脚本来使用unix shell脚本发送邮件.

以下脚本允许我拥有可变的消息体.

是否可以在下面的代码中包含可变主题部分?

#!/bin/bash
# Sending mail to remote user

sender="root@sped56.lss.emc.com"
receiver="root@sped56.lss.emc.com"
body="THIS IS THE BODY"
subj="THIS IS THE SUBJECT."


echo $body | mail $receiver -s "THIS IS THE SUBJECT" // this works fine
echo $body | mail $receiver -s $subj // ERROR - sends one mail with only
//"THIS" as subject and generates another error mail for the other three words 
Run Code Online (Sandbox Code Playgroud)

Aar*_*lla 18

你忘记了引号:

echo $body | mail $receiver -s "$subj"
Run Code Online (Sandbox Code Playgroud)

请注意,必须使用双引号(否则,不会展开变量).

现在的问题是:为什么双引号$subj而不是$body$receiver.答案是echo不关心参数的数量.因此,如果$body扩展为多个单词,echo则只需在其间打印一个空格即可打印所有单词.在这里,引号只有在你想要保留双倍空格时才有意义.

至于$receiver,这是有效的,因为它只扩展为一个单词(没有空格).它会破坏邮件地址,如John Doe <doe@none.com>.