linux - 继续收到“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”。此外,无论用户输入是否正确,每次都会运行“很高兴听到它”的回声线。请帮忙。谢谢
解决方案
只需在最后一个 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
,这是 更好的危险
推荐阅读
- angular9 - 如何在 ng5-slider 中创建时间 AM/PM 范围滑块
- asp.net - TS React:后续的属性声明必须具有相同的类型
- r - 根据 R 中的另一个排序列值对数据框进行排序
- java - 保存 SpringLiquibase 的 SQL 语句以备后用
- python-3.x - PyQt5 为多个相同类型的小部件重复代码
- python - 3D 列表到 Numpy 数组的转换不起作用,它给了我列表对象
- swift - 通过 AWS AppSync 记录更新、删除到 AWS DynamoDB 的数据有问题吗?
- python-3.x - 在 Python 中模拟装饰方法的正确方法
- python - 如何在 WIndows 10 主机 PC 上的 Docker 上使用带有 Browserfull(非无浏览器)的 Ubuntu 容器上的 Chrome GUI 运行 Python Selenium?
- regex - 在 PCRE 中编写正则表达式所需的支持