【问题标题】:Syntax error on line 16第 16 行的语法错误
【发布时间】:2015-08-28 15:08:24
【问题描述】:

对我来说,语法似乎写得正确,但我在第 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

【问题讨论】:

  • shellcheck.net 无法解析此内容,但勇敢地尝试并解释了它认为接近的内容。这在这里可能会有所帮助。

标签: macos shell scripting


【解决方案1】:

&lt;&lt; EOF 开头的here document 需要在它自己的一行的开头结束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 addressemail1@us.example.comemail2@uk.example.com 或类似的东西,那么它会更接近 OK。就我自己而言,我仍然更喜欢选项而不是参数。)

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

【讨论】:

  • 在另一个“不太复杂”的脚本中,电子邮件功能可以完美运行。现在我没有收到线路错误,但它什么也没做。 ls 命令效果很好。所以我很好奇错误代码是否没有被正确读取所以脚本没有继续?到目前为止,我感谢您的帮助!
  • 大概,您已经修复了rc 赋值中的空格等语法问题?当您使用sh -x yourscript.sh(带有任何相关参数)运行脚本时,您会得到什么?如果此代码在函数中,则可能需要在函数中打开跟踪:函数顶部的set -x;。带有测试的elif 应替换为简单的else;如果返回码不满足$rc != 0,那么它必须满足$rc = 0
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 2013-12-17
相关资源
最近更新 更多