0

I need help. Im new at bash shell programming, I have task to do. So, I need to make menu and in menu a submenu. I mean, then u press 5 (database management) it has to show submenu with 3 options. I tried some methods with if but I cant get it done. This is my code:

#!/bin/bash

PS3='Please enter your option: '
options=("Buy" "Sale" "Change data" "View cars" "Database management" "Calculate profits" "Sold cars" "Vechiles in stock" "Exit program")
select opt in "${options[@]}"
do
    case $opt in
    "Buy")
        echo "you chose choice 1"
        ;;
    "Sale")
        ;;
    "Change data")
        ;;
    "View cars")
        ;;
    "Database management")
    PS3='Please enter your option: '
    options=("1" "2" "3")
    select opt in "${options[@]}"
    do
    case $opt in
    "1")
        echo "you chose choice 1"
        ;;
    "2")
        echo "you chose choice 2"
    "3")
        echo "you chose choice 3"
        ;;
    "Calculate profits")
        ;;
    "Sold cars")
        ;;
    "Vechiles in stock")
        ;;
    "Exit program")
        break 
        ;;
    *)
        echo "invalid option $REPLY"
        ;;
    esac
done

If press 5, it has to pop out another menu with "Back up" "Delete" "Restore" Thank you.

8
  • 2
    so why don't you copy the whole thing over, add it to option 5 and change the text?
    – Rinzwind
    May 25, 2019 at 17:02
  • Okey I will try it
    – Aurimas
    May 25, 2019 at 17:35
  • But I get unexpected end of file. How should I end script under option 5?
    – Aurimas
    May 25, 2019 at 18:43
  • Syntax error, most likely. You should post the updated code…
    – karlsebal
    May 25, 2019 at 19:34
  • Updated. I did like he said.
    – Aurimas
    May 25, 2019 at 20:08

2 Answers 2

2

I'd suggest using a shell function for the submenu - as well as making the code more manageable, it allows you to define a local PS3 to make it easier for the user to keep track of which menu they're in. A minimal prototype might be something like

# submenu
submenu () {
  local PS3='Please enter sub option: '
  local options=("Sub menu item 1" "Sub menu item 2" "Sub menu quit")
  local opt
  select opt in "${options[@]}"
  do
      case $opt in
          "Sub menu item 1")
              echo "you chose sub item 1"
              ;;
          "Sub menu item 2")
              echo "you chose sub item 2"
              ;;
          "Sub menu quit")
              return
              ;;
          *) echo "invalid option $REPLY";;
      esac
  done
}

# main menu
PS3='Please enter main option: '
options=("Main menu item 1" "Submenu" "Main menu quit")
select opt in "${options[@]}"
do
    case $opt in
        "Main menu item 1")
            echo "you chose main item 1"
            ;;
        "Submenu")
            submenu
            ;;
        "Main menu quit")
            exit
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

Of course, you could define the main menu in a shell function as well if you prefer.

1
  • Thanks dude, I will try your way
    – Aurimas
    May 26, 2019 at 18:51
2

Late addition I know - you may be able to use this menu program that I'm working on:https://github.com/steveh250/Unix-Menu-Program/tree/1.11

Latest version: https://github.com/steveh250/Unix-Menu-Program

4
  • 1
    That's interesting! Please add the basic steps of how to install and use your program. As it currently is your answer might be removed as being link-only. Please see Why and how are some answers deleted? Oct 27, 2020 at 20:10
  • Million thanks for this!! Mar 5, 2021 at 8:24
  • @BeastOfCaerbannog - The updated page has instructions on how to build and use the menu. Thanks.
    – Steveh250
    Apr 25, 2021 at 21:02
  • 1
    @SriramKannan - I have updated the code since then and added the ability to use the simple character menu or a character graphic menu: github.com/steveh250/Unix-Menu-Program
    – Steveh250
    Apr 25, 2021 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .