2015-08-28 90 views
0

对我来说,语法似乎写得正确,但我得到了第16行的错误....任何想法?第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 

粘贴似乎已经搞乱了格式。下面是引擎收录链接:

http://pastebin.com/1ub1EupC

+0

http://shellcheck.net无法解析这个,但做出勇敢的尝试,试图解释它认为什么是接近。这可能会有所帮助。 –

回答

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()重新排序选项。如果这两个词email address[email protected][email protected]或类似的东西,那么这将是更近确定。至于我自己,我还是喜欢以前的参数选项。)

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

+0

在另一个不太复杂的脚本中,电子邮件功能非常完美。现在我没有得到线路错误,但它什么都不做。 ls命令很好用。所以我很好奇,如果错误代码没有正确读取,所以脚本不会继续?我很欣赏迄今为止的帮助! – davidfindley19

+0

大概你已经修复了语法问题,比如赋值给'rc'的空格?当你用'sh -x yourscript.sh'(用任何相关的参数)运行脚本时你会得到什么?如果此代码位于函数中,则可能需要在函数顶部的函数“set -x;”中启用跟踪。带有测试的'elif'应该用简单的'else'替换;如果返回代码不满足'$ rc!= 0',那么它必须满足'$ rc = 0'。 –

相关问题