【问题标题】:Keep receiving "Else: endif not found." error继续收到“Else: endif not found”。错误
【发布时间】:2019-04-11 18:41:20
【问题描述】:

这是我的脚本的提示: 询问用户“你还好吗?”的脚本

如果用户回复 y 或 Y,则说“很高兴听到它”,否则如果用户输入 n 或 N,则打印“抱歉,您感觉不舒服”。如果用户输入了其他字符,则打印不正确的选择并再次提问。

这是我所拥有的:

#! /bin/csh
echo "Are you OK? "
set n = $<
set loop = 1
if (("$n" == "y") || ("$n" == "Y")) then
    echo "glad to hear it"
else if (("$n" == "n") || ("$n" == "N")) then
    echo "sorry that you are not feeling good"
else
    echo "in-correct choice"
    while ( $loop == 1 )
      echo "Are you OK? "
      set n = $<
      if (("$n" == "y") || ("$n" == "Y")) then
        echo "glad to hear it"
        set loop = 0
      else if (("$n" == "n") || ("$n" == "N")) then
        echo "sorry that you are not feeling good"
        set loop = 0
      else
        echo "in-correct choice"
      endif
    end
endif

我不断收到错误消息“else: endif not found”。此外,无论用户输入是否正确,每次都会运行“很高兴听到它”的回声线。请帮忙。谢谢

【问题讨论】:

  • 嗯。拼写检查不支持csh。对不起。我没有看到你的代码有任何明显的问题,当我复制/粘贴到一个新文件并在本地运行它时,你的代码按预期工作。我对 csh 的体验(15 年前)让我认为你在一行的末尾有一个额外的字符,它会抛出 csh 的附加解析器。如果您使用的是 vi,请发出 cmd :set list。请注意,现在所有制表符都转换为^I,并且每行的末尾都用$ 字符标记。特别注意每一行的末尾,如果有任何多余的空格字符,请将它们删除...
  • .... 看看是否有帮助。也许快捷方式是将代码直接从 Q 复制/粘贴到一个新文件中,然后 chmod +x myScript.csh 并祈祷问题消失。我已经看到这种事情发生了很多次,这只是一个谜,重新创建(小心地)文件使问题消失了。祝你好运!
  • 哎呀,让shellcheck 不支持csh。祝你好运。

标签: linux csh


【解决方案1】:

只需在最后一个 endif 语句后添加新行:

...
    end
endif
#new line

或者我建议始终以退出状态结束 csh 脚本,你应该很高兴:

...
    end
endif
exit (0)

不管怎样,这里也对你的脚本进行了一点重写:

#!/bin/env csh
while (1)
  echo "Are you OK?"
  set n = "$<"
  if (("$n" == "y") || ("$n" == "Y")) then
    echo "glad to hear it"
    break
  else if (("$n" == "n") || ("$n" == "N")) then
    echo "sorry that you are not feeling good"
    break
  else
    echo "in-correct choice"
  endif
end
exit (0)
  • set n = $&lt; 这里有更好的危险set n = "$&lt;" 为了不处理例如字符串y abc 为是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2021-11-23
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2022-05-10
    • 2020-03-26
    相关资源
    最近更新 更多