I am writing a script in ksh which needs to check a variable is set to one of the two accepted values. I have got the following code but when I enter a valid value e.g. “C” I get the error message (Not a valid value):
if [[ "$Mode" != "C" ]] || [[ "$Mode" != "M" ]]
then
echo ""
echo "*Not a valid value*"
exit 2;
fi
I am not sure why it is failing on the string comparison, I have tried varations on syntax but still not getting anywhere, any help would be much appreciated.
I think this is the easier way to test multiple values. I like to think of it ‘if Mode is NOT in the list of C, or M, or ….; then ….’ I see beginners post code like yours (and I did it myself when I was a beginner), because then your mental model is ‘if Mode is NOT C OR Mode is NOT M then …’, but you you really need the ‘AND’ instead (as illustrated by jlliagre).
I hope this helps.