Reputation: 644
I am trying to create a script a Y/n prompt in bash which in case of hitting the ENTER key to execute the script. So far I have created the script to accept only Yes/No answers and to read only the first letter and ignore the rest:
while true; do
read -p "Do you wish to remove this directory [Y/n]? " rmv
rmv=${rmv,,} # lower the letters in the rmv variable
case $rmv in
[y]* ) echo "YES"; break;;
#[] ) echo "Enter Key"; break;;
[n]* ) echo "NO"; exit;;
* ) echo "Please answer yes or no! ";; # repeat until valid answer
esac
done
The idea is that in case of Y/y/yes/YES or Enter the script to execute some command, in case of No/N/n/no to break the loop and in case of invalid answer to ask the question again. I was thinking that the best will be to use OR "||" on the row with "Y" case.
Upvotes: 1
Views: 2321
Reputation: 4681
Inside your case
statement, you can test for the empty string like this:
"") echo "Enter Key"; break;;
Upvotes: 2