LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bashing on your door again!! (https://www.linuxquestions.org/questions/programming-9/bashing-on-your-door-again-214644/)

jay2901 08-07-2004 09:32 AM

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


jlliagre 08-07-2004 09:55 AM

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.

david_ross 08-07-2004 10:07 AM

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"
                        ;;


jay2901 08-07-2004 10:25 AM

This site as got the most helpful people on the planet, I swear!! Thanks guys!


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