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
syntaxerror
  • 2,236
  • 2
  • 27
  • 49
Krup
  • 45
  • 1
  • 5

2 Answers2

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
Nidal
  • 8,856
  • 11
  • 55
  • 74
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
glenn jackman
  • 84,176
  • 15
  • 116
  • 168