Alternative to elif in sh script?

Hello members,

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

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

I tried:

Code:
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.
 
By reading the sh() man page I guess that
Code:
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.
 
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:

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