# bourne: $var in ' )' part of 'case' statement



## Seeker (Mar 19, 2011)

This is part of a code

```
case "$driver" in
            $fw_devs )
                ...
```
Works:

```
fw_devs=ral
```
However, this doesn't:

```
fw_devs='ral | run'
fw_devs='ral|run'
```

Coping raw string from var, worked:

```
case "$driver" in
            ral|run )
                ...
```


----------



## DutchDaemon (Mar 19, 2011)

You cannot assign multiple values to a variable that way. This will only make this a _literal_ variable containing the string "ral|run" or "ral | run". You can use arrays (driver[0]=ral, driver[1]=run, echo ${driver[0]}, echo ${driver[1]}), but calling those from a case script the way you would like to may create more work than simply putting the multiple choices in the case script itself. There may be other solutions, of course.

Experiment with [cmd=]echo ${driver[*]}[/cmd] maybe. This will print the entire array.


----------



## wblock@ (Mar 19, 2011)

It can be done with an eval, as shown here: http://www.unix.com/shell-programming-scripting/90311-help-variable-substitution-case-statement.html.

Any time you find yourself trying to do something nontrivial in a shell script, stop and ask if there's a good reason you're using sh rather than something more powerful that can call sh (i.e., Perubython).


----------



## jilles@ (Mar 19, 2011)

Apart from eval, another trick that sometimes works is to reverse word and pattern, for example:

```
fw_devs='ral|run'
case "|$fw_devs|" in
*"|$driver|"*) echo yes ;;
*) echo no ;;
esac
```

Make sure $driver does not contain a "|" symbol.


----------



## Seeker (Mar 21, 2011)

@ DutchDaemon -> bourne shell doesn't have an arrays.
@ wblock -> Thx! I've put whole 'case' statement in eval "".
@ jilles -> Excellent hack! However, my 'case' statement, is too complex, for your hack.


----------

