Shell script goto?
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" |
Will you please show us your code so that we could suggest you something.
May be someone could suggest you something better IMHO. |
Code:
while [ x$CHEESE != "xN" ] && [ x$CHEESE != "xY" ] |
ilikejam likes cheese too? Thanks, it works great. One question though...
"x$CHEESE != "xN"" What do those "x"'s signify? Wouldn't it be looking for "xN", and not "N"? I don't think I've ever seen that in a script before... Thanks! PS: How would I make it so pressing enter on a user input field (that can be anything) doesn't kill my script? |
The comparison is x$Cheese != ... (notice the x.
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. |
Quote:
|
Quote:
Code:
while read |
I was always taught and believe that break statements like that are bad for the readability of code.
Code:
EXIT="always initialize" |
Quote:
Dave |
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
|
All times are GMT -5. The time now is 04:55 AM. |