LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2004, 09:32 AM   #1
jay2901
LQ Newbie
 
Registered: Aug 2004
Location: Warrington, Cheshire, UK
Distribution: Fedora Core 2
Posts: 6

Rep: Reputation: 0
Question Bashing on your door again!!


I am trying to get a test piece of code working, it is based around a simple menu system. I can not get a while loop to work within another while loop. Can you answer my question? And if you find the answer to that one can you tell me how to use the || operator so I can catch both uppercase and lowercase for "Y" and "y" in the menu.

Thank you in advance, here's the code not very exciting, just a tester for the while loop:

Code:
#!/bin/bash

flag="1"
while [ "$flag" -eq "1" ]; do
	clear
	echo "in the main menu"
	echo "EMPLOYEE DATA PROGRAM"
	echo "---------------------"
	echo " "
	echo "1. Add employee details"
	echo "2. View a list of employees"
	echo " "
	echo "Q. Quit"
	echo " "
	echo -n "Enter an option (1/2/Q) > "
	read MenuOpt
	case $MenuOpt in
		"1")
			again="y"
			while [ "$again" -eq "y" ]; do
			again=""
			echo " "
			echo "EMPLOYEE DATA PROGRAM - (Add Details)"
			echo "-------------------------------------"	
			echo " "
			echo -n "Enter employee's first name > "
			read FirstName
			echo -n "Enter employee's age > "
			read age
			echo "$FirstName" "$age" >> /home/jay/employees.dat	
			echo -n "Do you want to enter another > "
			read again
			done
			;;
		"2")
			clear 
			echo "View a list of employees"
			echo -n "Enter any character to continue > "
			read dummy
			;;
		"q")
			flag="0"
			;;
		"Q")
			flag="0"
			;;
		*)
			clear
			echo "Enter the correct selection"
			echo -n "Enter any letter to continue > "
			read dummy
	esac
	clear
done
 
Old 08-07-2004, 09:55 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You're confusing "-eq" that means numerically equal with "=" that check string equality, so
Code:
while [ "$again" -eq "y" ]; do
should be
Code:
while [ "$again" = "y" ]; do
or
Code:
while [ "$again" = "y" -o "$again" = "Y" ]; do
You can also use "==" instead of "=", just to be more consistent with other languages like C where "=" is reserved for assignation.

Don't confuse "||" from "-o", the former is to use between shell commands while the latter is for combining test (aka "[") clauses.
 
Old 08-07-2004, 10:07 AM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
I would usually use "==" for the while loop. Including OR it would be:
Code:
			while [ "$again" == "y" ] || [ "$again" == "Y" ]; do
For the OR in case:
Code:
		Q|q)
			flag="0"
			;;
 
Old 08-07-2004, 10:25 AM   #4
jay2901
LQ Newbie
 
Registered: Aug 2004
Location: Warrington, Cheshire, UK
Distribution: Fedora Core 2
Posts: 6

Original Poster
Rep: Reputation: 0
This site as got the most helpful people on the planet, I swear!! Thanks guys!
 
  


Reply



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
Revolving Selecting Packages door mark100net Slackware - Installation 1 03-26-2005 11:10 AM
Official door-to-door support? shade13 General 8 04-30-2004 03:10 PM
Linux server as a web door. Kaildathmar Linux - Networking 3 10-04-2003 03:28 PM
Can I kick windows out the door? Ben_Ty Linux - Software 12 07-15-2003 05:06 PM
Who's that Knocking at my door ! cjhills Linux - Newbie 4 01-17-2003 06:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 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
Open Source Consulting | Domain Registration