Bob Gardenier
Bob Gardenier

Reputation: 21

Unix case statement in OSX

I am trying to port some Windows bat files to Mac shell scripts

I get a syntax error when executing a file containing the following case statement:

  case ${1} in 
    ( C  | c   ) echo "hoera";; 

where argument ${1} is given as 'C'

The message is:

-bash: even: line 6: syntax error near unexpected token `newline'
'bash: even: line 6: `        ( C  | c   ) echo "hoera";; 

I think the syntax is correct according to the documentation. Where am I going wrong?

Upvotes: 1

Views: 2933

Answers (1)

Josh Jolly
Josh Jolly

Reputation: 11796

Syntax should be:

case $var in
  CASE1) COMMAND-LIST;;
  CASE2) COMMAND-LIST;;
esac

Each case consists of one or more shell patterns, separated by pipes - you don't need an opening parenthesis to match the closing one. Your example should instead be:

case ${1} in 
  C|c) echo "hoera";;

Or possibly:

case ${1} in 
  [Cc]) echo "hoera";; 

Upvotes: 3

Related Questions