4

I've searched some sites and also checked the syntax of the case command, but it didn't help. Where is my error?

#!/bin/sh
USER="user"
DIR="/linktodirectory"
###### script ######
case $1 in

As you can see above, i have a "in" there.

start)
su $USER -c "${DIR}/file start"
;;
stop)
su $USER -c "${DIR}/file stop"
;;
restart)
su $USER -c "${DIR}/file restart"
;;
status)
su $USER -c "${DIR}/file status"
;;
*)
echo "Usage: {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0

I'm using Ubuntu 16.04 64-bit.

HypnoTox
  • 41
  • 1
  • 4
  • 2
    Your script file probably contains DOS line endings, meaning that the parser sees `in\r` where it expects `in`. – chepner Oct 17 '16 at 20:52
  • 2
    Possible duplicate of [Why is a shell script giving syntax errors when the same code works elsewhere?](http://stackoverflow.com/questions/31886144/why-is-a-shell-script-giving-syntax-errors-when-the-same-code-works-elsewhere) – chepner Oct 17 '16 at 21:00

2 Answers2

1

I am suspecting a file encoding issue at first .

At first your syntax looks okay.. at least the line giving the error.

You could change the case line with double quotes as it has been said already.

If the problem persist , be sure to check file encoding.

hroussille
  • 419
  • 3
  • 12
0

Your first argument $1 should be enclosed in double quotes. Only then is this an argument to the case statement.

case "$1" in
Florian Heer
  • 694
  • 5
  • 17
  • 1
    The word following the keyword `case` is not subject to word-splitting or pathname expansion; quotes are not necessary. – chepner Oct 17 '16 at 20:51