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?

4
  • The code you posted does not cause the error.
    – choroba
    Aug 9, 2013 at 21:26
  • or have you composed this script in a Windows-based editor, and then copied it you your linux server without running dos2unix myPluginScript. ? Good luck.
    – shellter
    Aug 9, 2013 at 21:34
  • dos2unix does not work Aug 9, 2013 at 21:39
  • @user2649805 What do you mean that it does not work? Does that utility exist on your install or you still get the error?
    – Peter L.
    Aug 9, 2013 at 23:31

3 Answers 3

32

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

1
  • 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, 2023 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
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

1
  • 1
    to replace CRLF with LF
    – moudrick
    Oct 7, 2020 at 16:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.