Linux脚本的while循环中的用户输入



我正在编写一个脚本,它接受用户输入来启动while循环,现在我的语法是

echo -e "Would you like to run the script? (Y or N): c"
read ans
while [ $ans = "Y" ]
do
#the script

我的问题是如何让它接受多个输入?我希望脚本能够接受其他形式的Y,例如Yes、Yes或Y。我试图通过将代码更改为:来实现这一点

echo -e "Would you like to run the script? (Y or N): c"
read ans
while [ $ans = "Y","Yes","y","yes" ]
do
#the script

但它仍然给了我错误。有人知道怎么做吗?

echo -e "Would you like to run the script? (Y or N): c"
read ans
y_arr=("Y" "Yes" "y" "yes")
while [[ " ${y_arr[@]} " =~ " $ans " ]]; do echo 'y' && break; done;

将接受的表单存储在数组中,并检查其中是否有输入参数。

引用自";检查Bash数组是否包含值"0";

最新更新