2

I'm learning scripting to help with my work, and I'm just trying to wrap my head around using while loops.

I want to repeat a question if a user doesn't answer with Yes or No. Currently I have that working with an if conditional statement, that was easy, but it simply exits the script if the user doesn't answer with y or n.

I've tried various iterations of the below, figured out that I can't use -ne unless it's an integer, but what I can't seem to get right is the string comparison.

I found much better ways to do this online, but it's pointless to copy-paste those if I'm missing something basic about a simple while loop.

#!/bin/sh

while [ $CONFIRM != "^y"|"^n"] # This is where I'm stuck
do
echo "Please say Yes or No."   # Probably not doing this right either
read CONFIRM                   # Or this
done

if  echo "$CONFIRM" | grep -iq "^n" ; then
echo "Okay, stopping script."
else

#do some cool stuff

fi

Any and all advice appreciated.


And the correct answer is...

#!/bin/bash

shopt -s nocasematch

while ! [[ $CONFIRM =~ ^(y|n) ]]
do
echo "Please say Yes or No."
read CONFIRM
done

echo "Success!"
0

2 Answers 2

1

You are mixing regular expressions with pattern matching.

# Regular expression
while ! [[ $confirm =~ ^(y|n) ]]; do

or

# extended pattern
while [[ $confirm != @(y|n)* ]]; do 

should each do what you want.


The read command takes the name of a variable as its argument.

read confirm

Using a parameter expansion causes read to set the value of the variable whose name is contained in confirm:

$ confirm=foo
$ read $confirm <<< 3
$ echo "$foo"
3
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @chepner. As I'm using #!/bin/sh and looking for case-insensitive pattern matching, I should add that to get this to work I changed to #!/bin/bash and added shopt -s nocasematch above my while loop.
Ah, that read $confirm was oversight, should have spotted that!
0

Try something like this:

while [[ -z $confirm || ( $confirm != y && $confirm != n ) ]]; do
 read confirm
done

1 Comment

Worked perfectly, @rigglesbee, thank you! This is really useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.