1

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
  • Hey Julian. I recommend that you break this script up into small chunks. Run each chunk individually until you identify the source of the error. Commented Nov 25, 2009 at 4:22

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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 ].

Comments

2

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

3 Comments

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

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.