第16行的语法错误

时间:2015-08-28 15:08:24

标签: macos shell scripting

对我而言,语法似乎写得正确,但我在第16行得到错误....任何想法?

#!/bin/sh

#This is script is meant to be an add on to another one I'm using. Based on the return code output it will mail me the message.

read -p "What is the file name? " variable
rc = $?

ls -al $variable
if [[ $rc != 0 ]]; then
        mail -s "$variable script did not complete" email address -- -f email address \
        << EOF Your script has failed with error code $rc << EOF;
elif [[ $rc = 0 ]]; then
        mail -s "Your script completed" email address -- -f email address \
        << EOF Your script completed with error code $rc. << EOF;
fi
粘贴似乎搞砸了格式。这是pastebin链接:

http://pastebin.com/1ub1EupC

1 个答案:

答案 0 :(得分:1)

here document起始<< EOF需要在其自己的行的开头EOF结束。

    mail -s "$variable script did not complete" email address -- -f email address << EOF
Your script $variable has failed with error code $rc.
EOF

如果使用制表符(严格标签)进行缩进,则可以使用:

    mail -s "$variable script did not complete" email address -- -f email address <<-EOF
    Your script $variable has failed with error code $rc.
    EOF

(我很高兴地假设你的mail命令的语法没问题,虽然我不相信,即使使用GNU getopt()重新排序选项。如果这两个词{{1} } email addressemail1@us.example.com或类似的东西,那就更接近了。对于我自己,我仍然更喜欢选择之前的选项。)

我还希望将失败的脚本名称包含在电子邮件正文中以及主题行中。