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

我必须编写一个脚本来使用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\u0026gt;等邮件地址.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...