Skip to main content
whether || is treated as a syntax error is unspecified by POSIX. The sh implementation of zsh doesn't and does the expected thing instead.
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

There are two main issues in you script:

  1. The case statement is not closed by esac.
  2. The third pattern contains || which is a syntax error. in most Bourne-like shells (use '' or "" or an expansion that resolves to an empty value to match on the empty string portably)

It's unclear what your script is actually doing, so I'm speculating a bit and wrote this:

#!/bin/sh input="$1" case "$input" in D|P|Q|L1|L2) val='Hello' ;; DD|DE|DF|CA) val='Hi' ;; MM|KL|TK|SZMM|KL|""|TK|SZ) val='Bye' ;; *) echo 'error' >&2 exit 1 esac printf 'input was "%s", val is "%s"\n' "$input" "$val" 

Testing it:

$ ./script.sh D input was "D", val is "Hello" $ ./script.sh MM input was "MM", val is "Bye" $ ./script.sh BOO error 

There are two main issues in you script:

  1. The case statement is not closed by esac.
  2. The third pattern contains || which is a syntax error.

It's unclear what your script is actually doing, so I'm speculating a bit and wrote this:

#!/bin/sh input="$1" case "$input" in D|P|Q|L1|L2) val='Hello' ;; DD|DE|DF|CA) val='Hi' ;; MM|KL|TK|SZ) val='Bye' ;; *) echo 'error' >&2 exit 1 esac printf 'input was "%s", val is "%s"\n' "$input" "$val" 

Testing it:

$ ./script.sh D input was "D", val is "Hello" $ ./script.sh MM input was "MM", val is "Bye" $ ./script.sh BOO error 

There are two main issues in you script:

  1. The case statement is not closed by esac.
  2. The third pattern contains || which is a syntax error in most Bourne-like shells (use '' or "" or an expansion that resolves to an empty value to match on the empty string portably)

It's unclear what your script is actually doing, so I'm speculating a bit and wrote this:

#!/bin/sh input="$1" case "$input" in D|P|Q|L1|L2) val='Hello' ;; DD|DE|DF|CA) val='Hi' ;; MM|KL|""|TK|SZ) val='Bye' ;; *) echo 'error' >&2 exit 1 esac printf 'input was "%s", val is "%s"\n' "$input" "$val" 

Testing it:

$ ./script.sh D input was "D", val is "Hello" $ ./script.sh MM input was "MM", val is "Bye" $ ./script.sh BOO error 
Source Link
Kusalananda
  • 356.3k
  • 42
  • 737
  • 1.1k

There are two main issues in you script:

  1. The case statement is not closed by esac.
  2. The third pattern contains || which is a syntax error.

It's unclear what your script is actually doing, so I'm speculating a bit and wrote this:

#!/bin/sh input="$1" case "$input" in D|P|Q|L1|L2) val='Hello' ;; DD|DE|DF|CA) val='Hi' ;; MM|KL|TK|SZ) val='Bye' ;; *) echo 'error' >&2 exit 1 esac printf 'input was "%s", val is "%s"\n' "$input" "$val" 

Testing it:

$ ./script.sh D input was "D", val is "Hello" $ ./script.sh MM input was "MM", val is "Bye" $ ./script.sh BOO error