首页 > 解决方案 > 继续收到“Else: endif not found”。错误

问题描述

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

如果用户回复 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”。此外,无论用户输入是否正确,每次都会运行“很高兴听到它”的回声线。请帮忙。谢谢

标签: linuxcsh

解决方案


只需在最后一个 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 = $<set n = "$<"为了不处理例如字符串y abc,这是 更好的危险

推荐阅读