Check if a variable exists in a list in Bash

后端 未结 17 513
囚心锁ツ
囚心锁ツ 2020-11-29 17:08

I am trying to write a script in bash that check the validity of a user input.
I want to match the input (say variable x) to a list of valid values.

<
17条回答
  •  萌比男神i
    2020-11-29 17:36

    If it isn't too long; you can just string them between equality along a logical OR comparison like so.

    if [ $ITEM == "item1" -o $ITEM == "item2" -o $ITEM == "item3" ]; then
        echo In the list
    fi 
    

    I had this exact problem and while the above is ugly it is more obvious what is going on than the other generalized solutions.

提交回复
热议问题