Reputation: 31
This is similar to posts I have googled but I can't find an exact answer. I am trying to compare two strings ORed with another two string comparison:
elif [[ "$2" != "append" ]] || [[ "$2" != "replace" ]]
But the test fails even if append or replace is typed. Strangely enough, a single comparison works:
elif [[ "$2" != "append" ]]
So I know the problem is with the OR part, but am unable to fix the problem.
Upvotes: 0
Views: 49
Reputation: 10663
Also I suggest you to put it inside the brackets:
elif [[ $2 != 'append' && $2 != 'replace' ]]
Or you can use extglob to omit && completely:
shopt -s extglob
...
elif [[ $2 != @(append|replace) ]]
It seems like that is exactly what you expect!
Note that you don't have to quote the left side inside [[ ]]
brackets.
Upvotes: 0
Reputation: 361899
Use &&
instead of ||
.
elif [[ "$2" != "append" ]] && [[ "$2" != "replace" ]]
Every string will pass the test if you use ||
. There is no string that is equal to both append
and replace
.
I am looking for the expression to do the elif statements if $2 is neither "append" OR "replace".
Translating directly from English to code sometimes works, sometimes doesn't. Here it doesn't. You're really looking for a check that $2
is neither "append" NOR "replace". And there's no NOR operator. There is an AND operator, as in: check that $2
isn't "append" AND that it isn't "replace". That's a sentence that is translatable.
Upvotes: 3
Reputation: 75548
The conditioning is probably wrong.
[[ "$2" != "append" ]] || [[ "$2" != "replace" ]]
would always be true.
[[ "$2" != "append" ]]
would always be true if [[ $2 == replace ]]
or if $2
has any other value besides append
.
Upvotes: 3