Ubuntu: Creating nested menus in bash



Question:

I want to create more menus under option "a" when selected. If the user entered option "a" I want it to have a couple menus for you to select and for one of the options is to return to the "Main Menu". This is the 2 problems I'm having trouble with. I tried doing this same format I have below for the menu here and put it under "A" block of code but that didn't work.

#!/bin/bash    ok=0;    while ((ok==0))  do  echo "Main Menu:"  echo -e "\t(a) More Menu Options "  echo -e "\t(b) Exit"  echo -n "Please enter your choice:"  read choice  case $choice in      "a"|"A")      ok=1        ;;      "b"|"B")      exit      ;;          *)          echo "invalid answer, please try again"          ;;    esac  done  


Solution:1

Why not put the same cycle inside?

#!/bin/bash    while :  do  echo "Main Menu:"  echo -e "\t(a) More Menu Options "  echo -e "\t(b) Exit"  echo -n "Please enter your choice:"  read choice  case $choice in      "a"|"A")      while :      do      echo "Secondary menu"      echo -e "\t(c) Print this menu again"      echo -e "\t(d) Return to main menu"      echo -n "Please enter your choice:"      read choice1      case $choice1 in          "c"|"C")          ;;          "d"|"D")          break          ;;              *)              echo "invalid answer, please try again"              ;;      esac      done      ;;      "b"|"B")      exit      ;;          *)          echo "invalid answer, please try again"          ;;    esac  done  

There is also select command which creates menus:

select i in ant bee cat  do       echo $i      break  done  

Let's run it:

$ select i in ant bee cat; do echo $i; break; done  1) ant  2) bee  3) cat  #? 2  bee  


Solution:2

Try this:

#!/bin/bash    submanual(){  while :  do  echo "sub Menu:"  echo -e "\t(x) Options 1"  echo -e "\t(y) Options 1"  echo -e "\t(e) Back"  echo -n "Please enter your choice:"  read c  case $c in      "x"|"X")      # Options 1 and its commands      ;;      "y"|"Y")      # Options 2 and its commands      ;;      "e"|"E")      break      ;;          *)          echo "invalid answer, please try again"          ;;  esac  done  }    while :  do  echo "Main Menu:"  echo -e "\t(a) More Menu Options "  echo -e "\t(b) Exit"  echo -n "Please enter your choice:"  read choice  case $choice in      "a"|"A")      submanual      ;;      "b"|"B")      exit      ;;          *)          echo "invalid answer, please try again"          ;;    esac  done  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »