Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have several bash scripts I have made, and it's extremely inconvenient when there is a Y/N decision to be made, and if I make a typo or just hit enter it runs/messes up/exits the script. Is there a way to make it so "else" will go to the beginning of the question?
Example:
Do you like cheese? [Y/N]:
<read command is here>
If I press enter, or input something else and press enter the scripts messes up and exits instead of saying "invalid option"
The x is to guarantee that you will not be comparing against a null value.
Without the x, if you hit enter without keying anything, the shell would expand it to:
while [ != "N" ] && [ != "Y" ]
Just like in algebra, you can modify both sides of the equation the same way without upsetting its equality, but in this case you have to do it before shell expansion.
I was always taught and believe that break statements like that are bad for the readability of code.
Code:
EXIT="always initialize"
while [ "x$EXIT" != "xtrue" ]; do
echo -n "Make your selection (yes|NO): "
read SELECTION FOOBAR
case $SELECTION in
y*|Y*)
echo "You said \"Yes\""
EXIT=true
;;
*)
echo "I can't allow you to do that."
EXIT=false
;;
esac
done
Luckily for me, when I did the (ksh) course many yrs ago, the told us to stick with double brackets for various reasons, including null args as mentioned. There's other info here for bash http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.