0

I have been trying to compile my Bash script but I keep getting this syntax error even though I'm following the correct syntax.

Code:

    #!/bin/sh
set -u
SERVICE_NAME=Server
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/Server-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f "$PID_PATH_NAME" ]; then
             nohup java -cp '/home/ubuntu/ResumeParser/ResumeParser/ResumeTransducerbin/* :/home/ubuntu/ResumeParser/ResumeParser/GATEFiles/lib/*:/home/ubuntu/.../ServerTest' /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -cp '/home/ubuntu/ResumeParser/ResumeParser/ResumeTransducerbin/*:/home/ubuntu/... ServerTest' /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac
 

When I run: sudo service Server start

    /etc/init.d/Server: 9: /etc/init.d/Server: Syntax error: "then" unexpected     (expecting ";;")

When I run: bash -n Server

Server: line 9: syntax error near unexpected token `then'
Server: line 9: `        if [ ! -f $PID_PATH_NAME ]; then'

What am I doing wrong?

srao
  • 111
  • 1
  • 9
  • Do you have a `case ... esac` above the `if` statement? Perhaps `esac` is missing or eaten by some unclosed quotes. – Walter A Aug 11 '16 at 21:39
  • How are you getting one error "OR" another error? Why are you not just getting either one or both? – that other guy Aug 11 '16 at 21:39
  • SERVICE_NAME=Server PID_PATH_NAME=/tmp/Server-pid case $1 in start)         echo "Starting $SERVICE_NAME ..."      if [ ! -f "$PID_PATH_NAME" ]; then nohup java -cp '/home/ubuntu/.../ServerTest /tmp 2>> /dev/null >> /dev/null &                         echo $! > $PID_PATH_NAME             echo "$SERVICE_NAME started ..."         else             echo "$SERVICE_NAME is already running ..."         fi     ;; The error msg I see is - $ sudo service Server start /etc/init.d/Server: 9: /etc/init.d/Server: Syntax error: "then" unexpected (expecting ";;") – srao Aug 11 '16 at 21:48
  • @that other guy: I used "bash -n Server" to check for my bash error. That's how I got the second set of error msgs. – srao Aug 11 '16 at 21:49
  • @WalterA Yes I do have a case esac. – srao Aug 11 '16 at 21:50
  • @John1024 I apologize. I have edited my question and pasted the code I'm using there. Please have a look at it. – srao Aug 11 '16 at 21:55
  • @srao That is much better. Thank you. – John1024 Aug 11 '16 at 22:02
  • @srao The error message shown for `bash -n Server` does not match the code shown in the question. You may see the difference in code as small but small things can make a big difference in shell code. – John1024 Aug 11 '16 at 22:06
  • you have to add an argument: `bash -n Server start` or another problem arises. BTW I tested your shell in MSYS got no syntax errors. Can you run ANY other script without problems? – Jean-François Fabre Aug 11 '16 at 22:06
  • I tried `bash -n` on the code shown and also received no error message. – John1024 Aug 11 '16 at 22:11
  • 1
    @srao What editor did you use to create the script? A common cause of strange errors like this is an editor which inserts look-alike unicode characters in place of shell-useful ASCII characters. – John1024 Aug 11 '16 at 22:20
  • First thing to check: does your script have DOS line endings? See http://stackoverflow.com/tags/bash/info for some more debugging help. – chepner Aug 11 '16 at 22:38
  • 1
    Sublime text. It seems like there was a unicode non-breaking space from my editor. It works fine now! Thank you. – srao Aug 11 '16 at 22:39

3 Answers3

1

Not sure the value of $PID_PATH_NAME or try with if [ ! -f "$PID_PATH_NAME" ]; -> just to make sure PID_PATH_NAME not adding extra attributes/characters.

But I would suggest to run static tool on shell/bash scripts to find crazy/simple problems like this.

Static tool to verify shell scripts syntax

Balaji Reddy
  • 463
  • 4
  • 11
  • The tool helped me identify unicode non-breaking spaces that were inserted by my editor. Thank you! It works now! – srao Aug 11 '16 at 22:40
0

Crash can occur PID_PATH_NAME is empty/undefined or even has spaces in it

Shell sees that as if [ ! ]; then

Prefer

if [ ! -f "$PID_PATH_NAME" ]; then

In addition, if PID_PATH_NAME has a chance to be empty, instead of getting a strange behavior (testing if an empty filename exists is always false), you can check like this prior to testing if the file exists:

In that case, I have adapted it to your argument parsing, because if you pass no argument to your script it fails in a not very user-friendly fashion:

if [ -z "$1" ]; then
   echo "no command defined: should be start, restart, stop. Aborting"
   exit 1
fi
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • My thought exactly! File names are good to always surround with `"`. – svante Aug 11 '16 at 20:52
  • 1
    If you test them, you will find that `[ ! ]` and `[ ! -f ]` and `[ ! -f "" ]` are all valid test commands and do not cause errors. They may not be _useful_ test commands but they are not syntax errors that would cause an error message to print. – John1024 Aug 11 '16 at 21:01
  • Ok, so it's not the root cause. You're right, we should see the full file, must be something before then. – Jean-François Fabre Aug 11 '16 at 21:09
  • @Jean-FrançoisFabre I have updated my question and pasted the whole code I'm using there. Please have a look at it. – srao Aug 11 '16 at 21:59
0

you need to have

# Source function library.
. /etc/init.d/functions

before the case statement

Eric Aya
  • 69,473
  • 35
  • 181
  • 253