I am trying to compare the string stored in a variable with three different strings and if none of them match then throw an error. I've tried to do this in a single if statement using the logical operator OR. But every time I am getting error even though the value stored in the variable is same as one of the possible value. Please find the snippets I tried.
if [[ "$TYPE" != "LOCAL" || "$TYPE" != "REMOTE" || "$TYPE" != "BOTH" ]]; then
echo -e "\n\tINCORRECT OR NULL ARGUMENTS PASSED. PLEASE VERIFY AND CORRECT THE USAGE MENTIONED AS BELOW: \n"
Usage
exit 1
fi
if [[ "$TYPE" != "LOCAL" ]] || [["$TYPE" != "REMOTE" ]] || [["$TYPE" != "BOTH" ]]; then
echo -e "\n\tINCORRECT OR NULL ARGUMENTS PASSED. PLEASE VERIFY AND CORRECT THE USAGE MENTIONED AS BELOW: \n"
Usage
exit 1
fi
&&
not||
– pLumo Jul 27 '18 at 12:54TYPE
cannot simultaneously be equal toLOCAL
,REMOTE
, andBOTH
. In plain English, your use of OR says "If type is not local, OR if type is not remote, OR if type is not both, throw an error." What you probably want is "If type is not local, AND is not remote, AND is not both, then throw an error." See the difference? – Kevin Kruse Jul 27 '18 at 13:08NOT (this OR that OR theother)
is equivalent to(NOT this) AND (NOT that) AND (not theother)
-- this is one of De Morgan's laws of logic. – Gordon Davisson Jul 27 '21 at 10:24