19

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?

user2649805
  • 303
  • 1
  • 3
  • 7

3 Answers3

31

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 :)

Ni Xiaoni
  • 1,639
  • 2
  • 13
  • 10
  • I had this error and it had nothing to do with formatting (I tested my solution w/ & w/o Windows line endings). I would post a new answer but it's closed. Apparently, `sh` wants a `;` after closing `]` of condition, before the `then`, and another `;` before the `fi`. I had an `else` part and no other semicolons were needed. Not sure what you need without an `else` part. E.g. `if [ -f $A ]; \ then ls . \ else ls ..; fi` – dan Jul 17 '23 at 13:51
23
  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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
4

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

Pratik
  • 41
  • 1