This question already has answers here:
Closed 6 years ago.
bash regex with quotes?
(4个答案)
Why isn't this regular expression test working? [duplicate]
(3个答案)
我希望检查用户输入是否正确(伪代码)
userInput=""

#check until user enters 'y' OR 'Y' OR 'n' OR 'N'

while [[ "$userInput" != "[yYnN]" ]] ; do

     read userInput

done

我的想法是使用[]通配符进行匹配,但完成此任务的正确方法是什么?

最佳答案

有一种方法:

while true; do
case $userInput in
  y|Y|n|N)
    break
    ;;
  *)
    # read more input
    ;;
esac
done

如果您不关心可移植性,可以使用
while [[ ! $userInput =~ [yYnN] ]]; do

关于linux - 条件中的Bash脚本通配符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19760384/

10-14 18:07