bash script Syntax error: word unexpected (expecting "do")

12,011

Probably you managed to insert windows line endings when you copy and pasted. If you have dos2unix, use it. (dos2unix scriptfile) Otherwise, there are a number of similar utilities.

Share:
12,011
Author by

Undermine2k

Before: (╯°□°)╯︵ ┻━┻ After: ♪└(・。・)┐♪ ┬───┬

Updated on June 11, 2022

Comments

  • Undermine2k 6 months

    I'm trying to run this dropbox script on my nginx server , but im getting:

     Syntax error: word unexpected (expecting "do")
    

    I copy pasted the script for a website, and I tried removing special characters, but im still getting the same error.

    script:

    #!/bin/sh
    # /etc/init.d/dropbox
    ### BEGIN INIT INFO
    # Provides:          dropbox
    # Required-Start:    $network $syslog $remote_fs
    # Required-Stop:     $network $syslog $remote_fs
    # Should-Start:      $named $time
    # Should-Stop:       $named $time
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start and stop the dropbox daemon for debian/ubuntu
    # Description:       Dropbox daemon for linux
    ### END INIT INFO
    DROPBOX_USERS="root"
    start() {
        echo "Starting dropbox..."
        for dbuser in $DROPBOX_USERS; do
            start-stop-daemon -b -o -c $dbuser -S -x /home/$dbuser/.dropbox-dist/dropboxd
        done
    }
    stop() {
        echo "Stopping dropbox..."
        for dbuser in $DROPBOX_USERS; do
            start-stop-daemon -o -c $dbuser -K -x /home/$dbuser/.dropbox-dist/dropboxd
        done
    }
    status() {
        for dbuser in $DROPBOX_USERS; do
            dbpid=`pgrep -u $dbuser dropbox`
            if [ -z $dbpid ] ; then
                echo "dropboxd for USER $dbuser: not running."
            else
                echo "dropboxd for USER $dbuser: running."
            fi
        done
    }
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      restart|reload|force-reload)
        stop
        start
        ;;
      status)
        status
        ;;
      *)
        echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}"
        exit 1
    esac
    exit 0
    
  • Undermine2k over 8 years
    So this fixed the other error now i'm getting: /etc/init.d/dropbox: #: not found
  • rici
    rici over 8 years
    @Undermine2k: That will be the pseudo-BOM which Windows likes to insert at the beginning of a UTF-8 text file, unnecessarily IMHO. Using a text editor on the server, delete and retype the first line (#!/bin/sh).
  • Erwin Waterlander over 8 years
    Use dos2unix >= 6.0.0 and it will also remove the BOM.
  • Ram Rachum
    Ram Rachum almost 8 years
    Downvote for patronizing people who have a different perspective on root usage than you.