使用或门在单个 if 语句 shell 脚本中比较多个字符串

multiple string comparison in a single if statement shell script using OR gate

我正在尝试将存储在字符串变量中的值与三个不同的可能值进行比较,如果其中任何一个不匹配,则尝试在使用逻辑运算符 OR 的单个 if 语句中抛出错误。但是每次我都会收到错误,即使存储在变量中的值与可能的值之一相同。请找到我试过的片段。

    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

我想你想改用 AND (&&)。参见:

或:

irb(main):002:0> true || true || false
=> true

irb(main):003:0> true || true || true
=> true

irb(main):004:0> false || false || false
=> false

和:

irb(main):005:0> true && true && false
=> false

irb(main):006:0> true && true && true
=> true

irb(main):007:0> false && false && false
=> false

使用 ruby 仅用于演示