2

I'm trying to do the following :

function main_menu 
{
option=0
until [ "$option" = "4" ]; do
echo "  1.) Add user"
echo "  2.) Remove user"
echo "  3.) Update user"
echo "  4.) Quit"

echo -n "Enter choice: "
read option
echo ""
case $option in
    1 ) add_user ; press_enter ;;
    2 ) remove_user ; press_enter ;;
    3 ) update_user ; press_enter ;;
    4 ) exit;;
    * ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4; 
esac
done
 }

In a switch case(do while option is not 4).The 3rd option will show another sub menu with switch case as follows:

function update_user 
{
 option=0
 until [ "$option" = "3"]; do
 echo "  1.) Update username"
 echo "  2.) Update password"
 echo "  3.) Return to menu"

 echo -n "Enter choice: "
 read option
 echo ""
 case $option in
 1 ) update_username; press_enter ;;
 2 ) update_password; press_enter ;;
 3 ) main_menu; press_enter ;;
 4 ) exit;;
 * ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3; 
 esac
 done
  }

The 3rd option goes back to the main menu, but when I try to quit the sub menu keeps appearing.

Anyone can advise me on a better way?

5
  • Some code could be useful Commented Aug 2, 2014 at 13:58
  • @KonstantinV.Salikhov Updated on code Commented Aug 2, 2014 at 14:10
  • 3
    You can just use bash select builtin tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_06.html Commented Aug 2, 2014 at 14:13
  • @KonstantinV.Salikhov I'll try it out. Are there any other ways,though? Commented Aug 2, 2014 at 14:20
  • Start by cleaning up your code so it runs at all, and then posting. As written, your sub menu is never called. Commented Aug 2, 2014 at 14:22

2 Answers 2

2

As pointed out by @KonstantinV.Salikhov, menus in bash are what select loops are for.

Here's one way you can implement your menu using select:

main_menu () {
    options=(
        "Add user"
        "Remove user"
        "Update user"
        "Quit"
    )
    select option in "${options[@]}"; do
        case $option in
            ${options[0]})
                add_user
                break
            ;;
            ${options[1]})
                remove_user
                break
            ;;
            ${options[2]})
                update_user
                break
             ;;
            ${options[3]})
                exit
             ;;
            *) 
                echo invalid option
            ;;
        esac
    done
}
main_menu
Sign up to request clarification or add additional context in comments.

Comments

1

Replace two times $selection with $option, break with exit, remove } in function update_user, in function update_user add } after done and replace "$option" = "7" with "$option" = "4".


#!/bin/bash

function update_user 
{
 option=0
 until [ "$option" = "3"]; do
 echo "  1.) Update username"
 echo "  2.) Update password"
 echo "  3.) Return to menu"

 echo -n "Enter choice: "
 read option
 echo ""
 case $option in
 1 ) update_username; press_enter ;;
 2 ) update_password; press_enter ;;
 3 ) main_menu; press_enter ;;
 4 ) break ;;
 * ) tput setf 3;echo "Please enter 1, 2 or 3";tput setf 3; 
 esac
#   }
 done
}

function main_menu 
{
option=0
until [ "$option" = "4" ]; do
echo "  1.) Add user"
echo "  2.) Remove user"
echo "  3.) Update user"
echo "  4.) Quit"

echo -n "Enter choice: "
read option
echo ""
case $option in
    1 ) add_user ; press_enter ;;
    2 ) remove_user ; press_enter ;;
    3 ) update_user ; press_enter ;;
    4 ) exit;;
    * ) tput setf 4;echo "Please enter 1, 2, 3, or 4";tput setf 4; 
esac
done
 }

main_menu

4 Comments

The code runs,but if I replace break with exit won't it exit from the program? I'm trying to return to the sub menu.
Replace only break with exit in function main_menu.
That's what I tried though,but nothing happens. It doesn't go back to main menu.
I've added your code as a bash script in my answer. It works for me.

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.