在bash脚本意外"语法错误:文件&QUOT意外结束;在if语句 [英] in bash script unexpected "syntax error: unexpected end of file" on if statement

查看:177
本文介绍了在bash脚本意外"语法错误:文件&QUOT意外结束;在if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

W /母鸡我运行下面的code段

W/hen i run the following code snippet

#!/bin/bash
if [ "foo" = "foo" ];
then
echo "true"
else
echo "false"
fi
echo "end"

我得到

sfm_write_buffer_test.sh: line 9: syntax error: unexpected end of file

这没有任何意义。 echo语句工作正常,但遇到的if语句时,它提供了上述的错误。

this doesn't make any sense. echo statements works fine, but when the if statement is encountered it gives the above mentioned error.

推荐答案

您是在Cygwin,对吧?

You're on Cygwin, right?

正如我在评论说,当我复制和粘贴您的脚本,并在我的系统,它的工作原理运行;输出

As I said in a comment, when I copy-and-paste your script and run it on my system, it works; the output is

true
end

但是,当我改变从Unix样式的'\\ n'行结束到Windows风格的'\\ r \\ n',我得到你得到了同样的错误。

But when I change the line endings from the Unix style '\n' to the Windows style '\r\n', I get the same error you got.

随着Windows风格的行尾时,bash不会看到然后关键字;它看到了一个名为命令然后\\ r 。它从不尝试,因为它扫描匹配然后网​​络如果执行它关键字(其中承认,因为它不是在该行的结尾)。

With the Windows-style line endings, bash doesn't see the then keyword; it sees a command named then\r. It never tries to execute it because it's scanning for the matching then or fi for the if keyword (which it recognized because it's not at the end of the line).

请确保你的shell脚本使用Unix风格的行结束符。

Make sure your shell scripts use Unix-style line endings.

这篇关于在bash脚本意外"语法错误:文件&QUOT意外结束;在if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆