tags:

views:

944

answers:

4

Below is the snippet of code that keeps giving me the problem. Could someone explain to me why my code isn't working.

# Shell Version of Login Menu

Administrator ()
{
    clear
    ./Administrator.sh
}

# ------ Student User Menu ------
Student_Menu()
{
    clear
    ./studentMenu.sh
}

# ------ Borrow Menu ------
Borrow_Menu()
{
    clear
    ./BorrowBookMenu.sh
}

# ============================================
# ------ Main Menu Function Definition -------
# ============================================

menu()
{
echo ""
echo "1. Administrator"
echo ""
echo "2. Student user"
echo ""
echo "3. Guest"
echo ""
echo "4. Exit"
echo ""
echo "Enter Choice: "
read userChoice

if ["$userChoice" == "1"]
then 
    Administrator
fi

if ["$userChoice" == "2"]
then
    Student_Menu
fi


if ["$userChoice" == "3"]
then
    Borrow_Menu
fi

if ["$userChoice" == "4"]
then
    echo "GOODBYE"
    sleep
    exit 0
fi

echo
echo ...Invalid Choice...
echo 
sleep
clear
menu
}

# Call to Main Menu Function
menu
+1  A: 

As now formatted, the code works 'OK' with bash under Cygwin.

However, the logic in the menu function should be improved - using 'elif' to select alternative actions, and 'else' to deal with errors.

if [ "$userChoice" = 1 ]
then Administrator
elif [ "$userChoice" = 2 ]
then Student_User
elif [ "$userChoice" = 3 ]
then Borrow_Menu
elif [ "$userChoice" = 4 ]
then echo Goodbye; sleep 1; exit 0
else echo "Unrecognized choice $userChoice"; sleep 1; clear
fi

And then you can iterate the menu somewhere...

Jonathan Leffler
+1  A: 

do your menu like this

# Shell Version of Login Menu

Administrator ()
{
#     clear
    ./Administrator.sh
}

# ------ Student User Menu ------
Student_Menu()
{
#     clear
    ./studentMenu.sh
}

# ------ Borrow Menu ------
Borrow_Menu()
{
#     clear
    ./BorrowBookMenu.sh
}

# ============================================
# ------ Main Menu Function Definition -------
# ============================================

menu()
{
    while true
    do
        echo ""
        echo "1. Administrator"
        echo ""
        echo "2. Student user"
        echo ""
        echo "3. Guest"
        echo ""
        echo "4. Exit"
        echo ""
        echo "Enter Choice: "
        read userChoice
        case "$userChoice" in
            "1") Administrator ;;
            "2") Student_Menu ;;
            "3") Borrow_Menu ;;
            "4") echo "bye" ;exit ;;
             *) echo "Invalid";;
        esac
    done
}

menu
ghostdog74
Exactly what I was looking for but why was my menu throwing so many errors and weird statements?
Julian
One other thing, why did you comment out the "clear" command?
Julian
because i want to see what happens! you don't have to put it in during development.
ghostdog74
+3  A: 

You have an error in

if ["$userChoice" == "1"]
then 
    Administrator
fi

and the other similar if statements. You need to add a space after the [ and a space before the ]. In Bourne-like shells, [ is not part of the shell's grammar for conditionals, but a regular command which expects its last argument to be ].

Idelic
+3  A: 

Bash has a menu feature called "select":

#!/bin/bash
choices=(Administrator "Student user" Guest Exit)
savePS3="$PS3"
PS3="Enter choice: "
while :
do
    select choice in "${choices[@]}"
    do
        case $REPLY in
            1) clear
               ./Administrator.sh
               break;;
            2) clear
               ./studentMenu.sh
               break;;
            3) clear
               ./BorrowBookMenu.sh
               break;;
            4) echo "GOODBYE"
               break 2;;
            *) echo
               echo "Invalid choice"
               echo
               break;;
        esac
    done
done
PS3="$savePS3"

This is what the menu looks like:

1) Administrator
2) Student user
3) Guest
4) Exit
Enter choice: 3
Dennis Williamson