Using variable in case statement

I want to do this:

Ex 1:
case $answer in
  1|2|3|4|5) echo $answer;;
  x) break;;
  *) echo "Invalid selection. Try again.";;
esac

But I need the part "1|2|3|4|5" to be fetched from a variable, like so:

Ex 2:
case $answer in
  $cases) echo $answer;;
  x) break;;
  *) echo "Invalid selection. Try again.";;
esac

So if the input is 1-n, I want my program to execute (its not really echo I plan on executing, but I will send $answer as the action's input).

Is that possible? What I really want to know is, can the number of cases be variable depending on when the script is run? If so, how do I achieve it?

When written like in ex 2 my input is interpreted as * every time. I have also tried replacing "$cases" with "�echo $cases�" with the same result.

The script is written in bash and runs on Sun Solaris 9.

Try this

It should work.

Yes, that works. Thank you very much!