Linux 语法错误:文件结束意外(期望“then”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18155852/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:31:10  来源:igfitidea点击:

Syntax error: end of file unexpected (expecting "then")

linuxshellunixminecraft

提问by user2649805

I am making this code for a minecraft server plugin updater I am a new shell scripter so I don't know alot...
when I run this code I get a error:

我正在为 minecraft 服务器插件更新程序编写此代码我是一个新的 shell 脚本编写者,所以我不太了解...
当我运行此代码时,出现错误:

#!/bin/sh
export PATH=$PATH:.
#Options
PLUGINDOWNLOADLINK=http://api.bukget.org/3/plugins/bukkit/$PLUGINNAME/latest/download
# Plugin folder
if [ -f $PWD\plugins ]; then
PLUG=$PWD\plugins 
else
PLUG=$PWD\plug-ins
fi

cd $PLUG

if [ ! -f .\update ]; then
mkdir update
echo Making Directory "update"..
fi

# Plugins Found
    for i in $( ls );
    do
        PLUGINNAME=$i
    done
    cd .\update

    wget $PLUGINDOWNLOADLINK

    # No Plugins Found
    if [ ! -f $PLUG ]; then
    echo
echo
echo    No plugin found.
echo
echo
echo
echo   Plugins can be downloaded here:
echo   http://dev.bukkit.org/bukkit-plugins
fi

# stop 
sleep 3s
exit

I get this error:

我收到此错误:

Syntax error: end of file unexpected (expecting "then")

so I put "then" in the place it wanted me to and ran it again:
it gave me this error now:

所以我把“then”放在它想要我的地方并再次运行它:
它现在给了我这个错误:

Syntax error: end of file unexpected

I wrote it on windows 7 notepad++
how can I fix this?

我在 Windows 7 记事本 ++ 上写了它,
我该如何解决这个问题?

采纳答案by Ni Xiaoni

I have met the same problem. And the problem is the format of the file is "dos", but in linux shell requires "unix", so I install the "dos2unix"

我遇到了同样的问题。问题是文件的格式是“dos”,但在linux shell中需要“unix”,所以我安装了“dos2unix”

$ sudo apt-get install dos2unix

or if you use emacs, you can do this:

或者如果你使用 emacs,你可以这样做:

C-x RET f unix

Good luck :)

祝你好运 :)

回答by glenn Hymanman

  1. The Unix directory separator is forward slash not backslash.
  2. Does your editor on windows have an option to save files in "Unix" file format? The shell is seeing the word then^Mwith a carriage return on the end. If you don't have dos2unix then try running this command: sed -i 's/\r$//' filename
  1. Unix 目录分隔符是正斜杠而不是反斜杠。
  2. Windows 上的编辑器是否可以选择以“Unix”文件格式保存文件?外壳正在看到then^M结尾带有回车符的单词。如果您没有 dos2unix,请尝试运行以下命令:sed -i 's/\r$//' filename

回答by Pratik

It's because of file formatting. I faced the same problem. Just create another file with (.sh) extensions using gedit. Then paste the code into the new document. Hope this will resolve the issue.

这是因为文件格式。我遇到了同样的问题。只需使用 gedit 创建另一个扩展名为 (.sh) 的文件。然后将代码粘贴到新文档中。希望这能解决问题。

gedit file-name.sh

gedit 文件名.sh