Syntax error: unexpected end of file. Bash

4k views Asked by At

I want to set up a teamspeak bot, and I have this script to start this.

    #!/bin/bash
    if [ $1 = 'stop' ] 
      then 
        echo stop >> /root/ts3bot/tmp/log.txt
      date >>/root/ts3bot/tmp/log.txt
        echo ======================
        screen -S bot -X quit
      fi

    if [ $1 = 'start' ] 
      then 
      echo start >> /root/ts3bot/tmp/log.txt
      date >> /root/ts3bot/tmp/log.txt
      echo ======================
        screen -dmS bot php core.php
        ps ax | grep -v grep | grep -v -i SCREEN | grep links >> /root/ts3bot/tmp/log.txt
    fi
     <here is an extra blank line>

but when I type bash bot.sh it says syntax error: unexpected end of file I don't know what I did wrong :/ the chmod is set on 755

Thanks!

1

There are 1 answers

1
ghoti On

I suspect you may have copied this shell script from a Microsoft Windows box over to a Linux or Unix server. If so, the problem might be that you have DOS/Windows line endings, which can cause unpredictable results in scripts.

To check the script for bad line endings on a Linux or Unix server, you can dump the file (sort of like a hex dump) by typing the following at the shell prompt:

$ od -c bot.sh | less

And look for \n or \r or \r\n. If lines appear to have a \r at the end, then you've found the problem.

To FIX this line-ending problem, you can use a tool like dos2unix if it's installed on your system. If you don't have dos2unix but you're on a Linux server, you may be able to do this instead:

$ sed -i 's/\r//' bot.sh

to convert the file.

Lastly ... see the first line of the script, #!/bin/bash? Because of that, you don't need to run this with bash bot.sh, you can just execute it directly with ./bot.sh.