Jyothi Tulasi
Jyothi Tulasi

Reputation: 19

Case statement with multiple possible values ignored

I would like to execute a shell script which is having below code .

value=`echo "false"`
case $1 in "26492|26851|27407|26493")
     value=`echo "true"`
     ;;
esac

If the first argument is one of the values specified, a word true will be printed otherwise, false will be printed

26492|26851|27407|26493

I am not getting the required output.

I have executed like this sh -x script name 27407.

Can some one please help in this ?

Upvotes: 0

Views: 118

Answers (1)

einpoklum
einpoklum

Reputation: 131435

There are two issues here:

  1. You can't enclose the option indicators (the pipe symbols, |) within the quotation marks, since that makes bash treat that entire string as a single possible value.
  2. Your echo statement is enclosed in back-ticks, and that's assigned to value. So what happens is that instead of getting true as the output, you're getting the value of value changed.

Try:

case $1 in 26492|26851|27407|26493)
     echo "true"
     ;;
esac

Upvotes: 2

Related Questions