# Alternative to elif in sh script?



## amnixed (Jan 4, 2014)

Hello members,

is there an alternative to using elif when testing for multiple OR conditions? For example:


```
if [ $FILE = 01 ] && [ $MNAME != "Blick" ]
then
	echo "No match"
elif [ $FILE = 02 ] && [ $MNAME != "Flick" ]
then
	echo "No match"
...
fi
```

I tried:


```
if [ [ $FILE = 01 ] && [ $MNAME != "Blick" ] || [ $FILE = 02 ] && [ $MNAME != "Flick" ] ]
then
	echo "No match"
fi
```

... but I get the unexpected operator error message.

Thanks for your help.


----------



## ondra_knezour (Jan 4, 2014)

By reading the sh() man page I guess that

```
if [ { [ $FILE = 01 ] && [ $MNAME != "Blick" ]; } || { [ $FILE = 02 ] && [ $MNAME != "Flick" ] ; }]
then
   echo "No match"
fi
```
 may help, but I did not tried.

The && and || are list operators, which executes second command depending on return value of first command, so AND operations are grouped together with { ; } list for OR operator. Same outcome should produce usage of the () operator, but new subshell will be forked, so it is little less efficient.


----------



## worldi (Jan 4, 2014)

amnixed said:
			
		

> I get the unexpected operator error message.



That's because [ is a not part of the shell's language but a command (see `ls -l /bin/?`). It is the same as test().

You could use something like this:


```
if  [ "$FILE" = 01 -a  "$MNAME" != "Blick" ] || [ "$FILE" = 02  -a  "$MNAME" != "Flick" ]
then
   echo "No match"
fi
```


----------



## amnixed (Jan 4, 2014)

Thank you both - that was exactly what I was looking for (but I was too lazy to RTFM).


----------

