Reputation: 75
I want to check if the user has not entered 'y' or 'n' and if not then to keep looping asking the user to entered the correct letter but it is not working... the code below shows what I have tried so far ....can someone help me please???
echo "Enter 'y' to exit or 'n' to continue"
echo -n "Do you want to exit? "
read character
while [ "$character" != "y" || "$character" != "n" ];
do
echo -n "Wrong key re-enter 'y' to exit or 'n' to continue"
read character
done
Upvotes: 2
Views: 4661
Reputation: 4221
This approach tries to account for more user possibilities and still accomplish the same thing.
#if the user's input is not Y or N, Yes or No, y or n, yes or no
while [[ ! "$character" =~ ^([yY][eE][sS]|[yY])$ ]] && [[ ! "$character" =~ ^([nN][oO]|[nN])$ ]]
do
echo -n "Wrong key re-enter 'y' to exit or 'n' to continue"
read character
done
Upvotes: 0
Reputation: 75
Thanks a lot to all of you... after a good persistence and resilience I finally found the answer of what I was looking for... A posted the code below:
#if the user's input is not Y or N
while [[ $(read -sn1; echo ${character^^}) =~ [^YN] ]];
do
echo -n "Re-enter 'y' to exit or 'n'to continue: ?"
read character
done
Upvotes: 1
Reputation: 2237
you can just:
while [[ $(read -sn1 character; echo ${character^^}) =~ [^YN] ]]; do
echo -n "Wrong key ..."
done
Upvotes: 1