0

I am writing a script that accepts a user input to start a while loop, right now my syntax for it is

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

My question is how can I make this accept multiple inputs? I would like for the script to be able to accept other forms of Y, such as Yes, yes, or y. I tried to this by changing the code to:

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

but it still gave me errors. Anyone have an idea how to do this?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0
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;

Store the accepted forms in an array, and check whether the input parameter in them.

Reference from "Check if a Bash array contains a value"

Victor Lee
  • 2,467
  • 3
  • 19
  • 37