Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 6996  / 2 Years ago, wed, september 14, 2022, 5:34:59

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 " (a) More Menu Options "
echo -e " (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

More From » bash

 Answers
7

Why not put the same cycle inside?



#!/bin/bash

while :
do
echo "Main Menu:"
echo -e " (a) More Menu Options "
echo -e " (b) Exit"
echo -n "Please enter your choice:"
read choice
case $choice in
"a"|"A")
while :
do
echo "Secondary menu"
echo -e " (c) Print this menu again"
echo -e " (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

[#26325] Thursday, September 15, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diffeah

Total Points: 78
Total Questions: 130
Total Answers: 98

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;