LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2013, 12:34 PM   #1
jefferj54
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Rep: Reputation: Disabled
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
 
Old 09-01-2013, 12:41 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Use break on the part where you want to exit.
 
Old 09-01-2013, 01:02 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And please use [code][/code] tags
 
Old 09-01-2013, 02:00 PM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 4,081
Blog Entries: 1

Rep: Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527
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)
 
Old 09-01-2013, 02:07 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,115

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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

Last edited by haertig; 09-01-2013 at 02:09 PM.
 
1 members found this post helpful.
Old 09-01-2013, 08:18 PM   #6
jefferj54
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
How to quit continuous "while" loop

Thanks to all, especially Haertig, who really got it done.

joe.
 
Old 09-02-2013, 03:53 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please remember to mark as SOLVED once you have a solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Debian 6 dual boot with Win 7: No option for "Use largest continuous free partition" shivaa Debian 26 04-01-2013 01:57 AM
Xfce's Quit's "Save session for future logins" default catkin Linux - Desktop 4 09-02-2009 02:47 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM
Open gl programs quit, nvidia driver "permissions are too restrictive." sirbrett Slackware 4 10-26-2004 09:48 PM


All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration