我必须编写一个脚本来使用 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
最佳答案
你忘了引号:
echo $body | mail $receiver -s "$subj"
注意一定要用双引号(否则变量不会展开)。
现在的问题是:为什么在 $subj
周围加双引号而不是 $body
或 $receiver
.答案是 echo
不关心参数的数量。所以如果$body
扩展为几个词,echo
只会打印所有这些,中间有一个空格。在这里,引号只有在您想保留双空格时才重要。
至于$receiver
,这是有效的,因为它只扩展到一个单词(没有空格)。它会中断像 John Doe <doe@none.com>
这样的邮件地址.
关于bash - 使用 unix shell 脚本发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1781989/