且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Shell 脚本语法错误:意外的文件结尾

更新时间:2022-03-18 04:12:47

请注意,自撰写此答案以来,原始帖子已被编辑并已重新格式化.您应该查看历史记录以查看原始格式以了解此答案的上下文.

Note that the original post has been edited since this answer was written and has been reformatted. You should look at the history to see the original formatting to understand the context for this answer.

当你有不匹配的结构时经常会发生这个错误——也就是说,你没有匹配的双引号,匹配的单引号,没有关闭一个控制结构,比如一个缺失的 fi>if,或者缺少带有 fordone.

This error occurs often when you have mismatched structure - that is, you do not have matching double quotes, matching single quotes, have not closed a control structure such as a missing fi with an if, or a missing done with a for.

发现这些问题的***方法是使用正确的缩进,这将显示控制结构损坏的位置,以及语法突出显示,这将显示引号不匹配的位置.

The best way to spot these is to use correct indentation, which will show you where you have a broken control structure, and syntax highlighting, which will show you where quotes are not matched.

在这种特殊情况下,我可以看到您缺少一个 fi.在代码的后半部分,您有 5 个 if 和 4 个 fi.但是,您还有许多其他问题 - 您的反引号 touch/tmp/alert.txt... 命令在语法上无效,并且您需要在 if 测试.

In this particular case, I can see you are missing a fi. In the latter part of your code, you have 5 ifs and 4 fis. However you also have a number of other problems - your backquoted touch /tmp/alert.txt... command is syntactically invalid, and you need a space before the closing bracket of an if test.

清理你的代码,错误开始突出.

Clean up your code, and errors start to stand out.