继续收到“其他:找不到结尾”。错误

时间:2019-04-11 18:41:20

标签: linux csh

这是我的脚本的提示: 询问用户“您确定吗?”的脚本

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

1 个答案:

答案 0 :(得分:2)

只需在您的最后一个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为是