How to quit continuous "while" loop
I put this script together and would like to close the script by typing q to exit cleanly. Please do not recommend me doing this by "case." I know how to do this through "case." I'm doing this through "if" and want to keep it that way. My problem is when I press q,
I get this: q This month does not exist. Choose another month or q to quit. mint ~ # I would like if it is humanly possible to not see the above, which are parts of the script. I'd like clean exit when typing the q key. I'm submitting the present script below, and I ask that you not treat me as a novice, as I am new to this and trying to learn. Thank you, joe. echo "Please choose a month from January to December and that month will display which holiday is related to that month." while [ "$month" != "q" ] do read month if [ "$month" = "January" ] then echo "Martin Luther King Day is in the month of January." elif [ "$month" = "February" ] then echo "President's Day is in the month of February." elif [ "$month" = "March" ] then echo "There is no holiday in the month of March." elif [ "$month" = "April" ] then echo "There is no holiday in the month of April." elif [ "$month" = "May" ] then echo "Memorial Day is in the month of May." elif [ "$month" = "June" ] then echo "There is no holiday in the month of June." elif [ "$month" = "July" ] then echo "The Fourth of July is in the month of July." elif [ "$month" = "August" ] then echo "There is no holiday in the month of August." elif [ "$month" = "September" ] then echo "Labor Day is in the month of September." elif [ "$month" = "October" ] then echo "Columbus Day is in the month of October." elif [ "$month" = "November" ] then echo "Thanksgiving one of the most wonderful holidays, is in the month of November." elif [ "$month" = "December" ] then echo "Christmas is in the month of December." else echo "This month does not exist." fi echo "Choose another month or q to quit." done |
Use break on the part where you want to exit.
|
And please use [code][/code] tags
|
Code:
|
Your script exits when you hit 'q', but not until after it has printed the bogus "This month does not exist" message. This is because you "read month" at the top of the loop, print out the message at the bottom of the loop, and it gets printed BEFORE you loop back to the check if a 'q' was entered. Something like the following works better:
Code:
#!/bin/bash |
How to quit continuous "while" loop
Thanks to all, especially Haertig, who really got it done.
joe. |
Please remember to mark as SOLVED once you have a solution
|
All times are GMT -5. The time now is 08:40 PM. |