Bash 语法错误:文件意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6366530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash syntax error: unexpected end of file
提问by markcruz
Forgive me for this is a very simple script in Bash. Here's the code:
请原谅我这是一个非常简单的 Bash 脚本。这是代码:
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: dos2unix file.sh
host database username"
exit 0
fi
after running sh file.sh:
运行 sh file.sh 后:
syntax error: unexpected end of file
语法错误:意外的文件结尾
回答by clyfish
I think file.sh is with CRLF line terminators.
我认为 file.sh 带有 CRLF 行终止符。
run
跑
sudo apt-get install dos2unix
then the problem will be fixed.
那么问题将得到解决。
You can install dos2unix in ubuntu with this:
您可以使用以下命令在 ubuntu 中安装 dos2unix:
die () { test -n "$@" && echo "$@"; exit 1 }
回答by ulidtko
Another thing to check (just occured to me):
另一件事要检查(刚刚发生在我身上):
- terminate bodies of single-line functions with semicolon
- 用分号终止单行函数体
I.e. this innocent-looking snippet will cause the same error:
即这个看似无辜的片段会导致同样的错误:
die () { test -n "$@" && echo "$@"; exit 1; }
To make the dumb parser happy:
为了让愚蠢的解析器开心:
set -x
trap read debug
回答by marengaz
i also just got this error message by using the wrong syntax in an if
clause
我也刚刚通过在if
子句中使用错误的语法而收到此错误消息
else if
(syntax error: unexpected end of file)elif
(correct syntax)
else if
(语法错误:文件意外结束)elif
(正确的语法)
i debugged it by commenting bits out until it worked
我通过注释位来调试它,直到它工作
回答by theRiley
an un-closed if => fi clause will raise this as well
未关闭的 if => fi 子句也会引发此问题
tip: use trap to debug, if your script is huge...
提示:如果您的脚本很大,请使用陷阱进行调试...
e.g.
例如
export SHELLOPTS
set -o igncr
回答by Jay Killeen
I got this answer from this similar problem on StackOverflow
我从StackOverflow 上的这个类似问题中得到了这个答案
Open the file in Vim and try
在 Vim 中打开文件并尝试
:set fileformat=unix
:set fileformat=unix
Convert eh line endings to unix endings and see if that solves the issue. If editing in Vim, enter the command :set fileformat=unix and save the file. Several other editors have the ability to convert line endings, such as Notepad++ or Atom
将 eh 行结尾转换为 unix 结尾,看看是否能解决问题。如果在 Vim 中编辑,输入命令 :set fileformat=unix 并保存文件。其他几个编辑器具有转换行尾的能力,例如 Notepad++ 或 Atom
Thanks @lemongrassnginger
谢谢@lemongrassnginger
回答by zzapper
on cygwin I needed:-
在 cygwin 上我需要:-
cat > temp.txt < EOF
some content
EOF
in .bash_profile . This way I didn't need to run unix2dos
在 .bash_profile 中。这样我就不需要运行 unix2dos
回答by Rafael Urena
So I found this post and the answers did not help me but i was able to figure out why it gave me the error. I had a
所以我找到了这篇文章,答案对我没有帮助,但我能够弄清楚为什么它给了我错误。我曾有一个
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash fi
The issue was that i copied the above code to be in a function and inadvertently tabbed the code. Need to make sure the last EOF is not tabbed.
问题是我将上面的代码复制到一个函数中,并无意中将代码标记为标签。需要确保最后一个 EOF 没有被标签化。
回答by Sergey
I had the problem when I wrote "if - fi" statement in one line:
当我在一行中写下“if - fi”语句时遇到了问题:
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
Write multiline solved my problem:
写多行解决了我的问题:
run() {
echo hello
}
run()
回答by Christian Scott
This was happening for me when I was trying to call a function using parens, e.g.
当我尝试使用括号调用函数时,这发生在我身上,例如
run() {
echo hello
}
run
should be:
应该:
##代码##回答by Juniar
FOR WINDOWS:
对于 Windows:
In my case, I was working on Windows OS and I got the same error while running autoconf.
就我而言,我在 Windows 操作系统上工作,并且在运行 autoconf 时遇到了同样的错误。
- I simply open configure.ac file with my NOTEPAD++IDE.
Then I converted the File with EOLconversion into Windows (CR LF)as follows:
EDIT -> EOL CONVERSION -> WINDOWS (CR LF)
- 我只是用我的NOTEPAD++IDE打开 configure.ac 文件。
然后我将带有EOL转换的文件转换为Windows(CR LF),如下所示:
编辑 -> EOL 转换 -> WINDOWS (CR LF)