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!"