LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to quit continuous "while" loop (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-quit-continuous-while-loop-4175475472/)

jefferj54 09-01-2013 11:34 AM

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

konsolebox 09-01-2013 11:41 AM

Use break on the part where you want to exit.

grail 09-01-2013 12:02 PM

And please use [code][/code] tags

GazL 09-01-2013 01:00 PM

Code:


while read -p "month (or q to quit)? " month
 do
  if [ "$month" = "q" ]; then
    break
  fi
  echo "rest of stuff"
 done

It's best to loop on the 'read' rather than a test condition as it will also pick up any failure in the read command, and respond correctly to a ctrl-d (EOF)

haertig 09-01-2013 01:07 PM

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
echo -n "Enter month: "
read month
while [ "$month" != "q" ]
do
        if [ "$month" = "January" ]; then
                echo "You entered January"
        elif [ "$month" = "February" ]; then
                echo "You entered February"
        else
                echo "You didn't enter a known month or a q"
        fi
        echo -n "Enter month: "
        read month
done


jefferj54 09-01-2013 07:18 PM

How to quit continuous "while" loop
 
Thanks to all, especially Haertig, who really got it done.

joe.

grail 09-02-2013 02:53 AM

Please remember to mark as SOLVED once you have a solution


All times are GMT -5. The time now is 08:40 PM.