Check if a variable exists in a list in Bash

后端 未结 17 504
囚心锁ツ
囚心锁ツ 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条回答
  •  独厮守ぢ
    2020-11-29 17:45

    how about

    echo $list | grep -w -q $x
    

    you could either check the output or $? of above line to make the decision.

    grep -w checks on whole word patterns. Adding -q prevents echoing the list.

提交回复
热议问题