I am making a bash script with two case statements for creating a menu and its follow-up sub-menu.
My main menu is structured like this: Here, any selection is redirected to the SubMenu function.
Main-menu() {
select INPUT in "Apple" "Banana" "Kiwi" "Coconut" "Pasta" "Exit"
do
case $INPUT in
"Apple") echo "You selected: $INPUT";SubMenu;;
"Banana") echo "You selected: $INPUT";SubMenu;;
"Kiwi") echo "You selected: $INPUT";SubMenu;;
"Coconut") echo "You selected: $INPUT";SubMenu;;
"Pasta") echo "You selected: $INPUT";SubMenu;;
"Exit") echo "Goodbye..";exit;;
*) echo -e "Please enter valid number";;
esac
done
}
My sub-menu is structured similarly: Here, the selection is redirected to a function (FruitType,etc.).
SubMenu() {
echo "Which Category should this be under? Select from options below: [F,N,D,E]"
select type in "Fruits" "Nuts" "Dinner" "Exit"
do
case $type in
"Fruits") echo "You selected: $type";FruitType;;
"Nuts") echo "You selected: $type";NutsType;;
"Dinner") echo "You selected: $type";DinnerType;;
"Exit") echo "Goodbye..";exit;;
*) echo -e "Please enter a valid letter";;
esac
done
}
** The question here is for my SubMenu. I need to have [F,N,D,E] as my options for my input but it displays numeric options [1,2,3,4]. **
I have seen some examples of OPTARG and options like this:
while getopts u:p: option; do
case $option in
u) user=$OPTARG;;
p) pass=$OPTARG;;
esac
done
But here, I have to enter -u instead of u as my input. Also the options aren't displayed. I need to have the SubMenu options displayed and a letter as input.
Any ideas?
select
is hardcoded to use numbers, but you can just use read -p
instead without it:
while read -r -p $'Which category should this be under?\nSelect from options: [F]ruit, [N]uts, [D]inner, [E]ggs -- or [Q]uit:' type
case ${type^^} in
F|FRUIT) echo "You selected fruit";;
N|NUTS) echo "You selected nuts";;
D|DINNER) echo "You selected dinner";;
E|EGGS) echo "You selected eggs";;
Q|QUIT) echo "Done with the loop"; break;;
*) echo "Invalid selection";;
esac
done