3

I was given the the Task to create a menu in Linux but it appears it's not coming out great. When I select something I want to be able to perform that task then go back to the main menu when I finish.

#!/bin/bash

PS3='What do you want to do day: '
options=("Create Group" "Delete Group" "Create User 3" "Delete User" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Create Group")
            echo "You chose Create Group 1"
            ;;
        "Delete Group")
            echo "You chose to Delete Group"
            ;;
        "Create User")
            echo "You chose to Create User"
            ;;
        "Delete User")
            echo "You choose to Delete User"
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done
  • Remove " 3" and " 1". – Cyrus Jul 17 '14 at 4:53
1

you can do this using while with a permanent true condition:

while [ 1 -eq 1 ]  # or while [ true ] ( as glenn said)
do
 select opt in "${options[@]}"
 do
 case $opt in
     "Create Group")
         echo "You chose Create Group 1"
         ;;
     "Delete Group")
         echo "You chose to Delete Group"
         ;;
     "Create User")
         echo "You chose to Create User"
         ;;
     "Delete User")
         echo "You choose to Delete User"
         ;;
     "Quit")
         break
         ;;
     *) echo invalid option;;
  esac
 done
done
  • Or, more obviously, while true; do ... – glenn jackman Jul 17 '14 at 1:02
1

I assume you want to display the menu each time. Try this:

quit=false
until $quit; do
    select opt in "${options[@]}"; do
        case $opt in
            "Create Group")
                echo "You chose Create Group 1"
                break
                ;;
            "Delete Group")
                echo "You chose to Delete Group"
                break
                ;;
            "Create User")
                echo "You chose to Create User"
                break
                ;;
            "Delete User")
                echo "You choose to Delete User"
                break
                ;;
            "Quit")
                quit=true
                break
                ;;
            *) echo invalid option;;
        esac
    done
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.