Syntax error: end of file unexpected (expecting “then”)

流过昼夜 提交于 2020-01-11 02:07:18

问题


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:

#!/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:

Syntax error: end of file unexpected

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


回答1:


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"

$ sudo apt-get install dos2unix

or if you use emacs, you can do this:

C-x RET f unix

Good luck :)




回答2:


  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^M with a carriage return on the end. If you don't have dos2unix then try running this command: sed -i 's/\r$//' filename



回答3:


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 file-name.sh



来源:https://stackoverflow.com/questions/18155852/syntax-error-end-of-file-unexpected-expecting-then

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!