一个<<-EOF引发的血案

lesson learnt:
改之前先备份,复制旧的注释掉再加新的。
基础不牢,地动山摇。

故事是这样的,从网上拨拉下来一个定时发邮件的shell,用到了here document写正文。新功能是再加个邮件列表到cc里去。没想到这么小个改动竟然翻车了。出来混迟早要还的。原来的脚本长这样( 应该差不多长这样,我要是记得就回滚了
/bin/mail -s "${subject}" "${receiver}" <<-EOF
内容手动马赛克
EOF
加cc的时候手欠把<<-EOF改了,然后脚本就需要交互了
/bin/mail -s "${subject}" "${receiver}" -c "${cc}"
内容手动马赛克
<<-EOF
试着把-c参数改个位置
/bin/mail -s "${subject}" -c "${cc}" "${receiver}"
内容手动马赛克
<<-EOF
然并软
以为是<<-EOF换行了,删了再写还是那个位置,是terminal切割了并没有回车字符。
OK, fine,let's 度娘。大家原来都是这么写的
/bin/echo "内容手动马赛克" | /bin/mail -s "${subject}" -c "${cc}" "${receiver}"
好丑,我先用为敬。

做为一个完美主义不能忍,趁着夜深人静没有报警换bing搜一搜。换了几个姿势,哦不关键词,bing跟度娘差不多蠢,只知道宅男不懂攻城师。

来自力更生,继续换姿势,不要问我改了什么,辣眼睛

“我好像为了装逼用vi改的,自己都不记得改了什么”

supportJobCollection.sh: line 9: /bin/mail: Permission denied

扑街

➜ ~ ./supportJobCollection.sh
./supportJobCollection.sh: line 9: syntax error near unexpected token &lt;&lt;-'<br/>./supportJobCollection.sh: line 9:/bin/mail -s "${subject}" -c "${cc}" "${receiver}" | cat >> <<-EOF'

果断上ladder用我大谷哥。
歪国人的月亮就是比较圆,各种姿势真贴心。
https://www.cyberciti.biz/faq/linux-unix-bash-ksh-csh-sendingfiles-mail-attachments/
Tip #2: Writing Mail Body Using Here documents

The here documents (redirection) tells the shell to read input from the current source (HERE) until a line containg only word (HERE) is seen:

#!/bin/bash
...
....
mail -s "Disk Failed" [email protected]<<EOF
NAS server [ mounted at $(hostname) ] is running out of disk space!!!
Current allocation ${_SPACE} @ $(date)
EOF
...

什么echo + EOF cat + EOF,我个智障。

PS,中间还有个插曲,改成echo后内容不能回车换行。我竟然无耻得百度了才知道要echo -e。保持对shell的敬畏,什么了解熟悉都不要随便乱说。

PPS,我真是折腾的命,什么东西我都能给玩坏。

猜你喜欢

转载自blog.51cto.com/jerkou/2465386