嗨我需要一些帮助我在bash脚本编程脚本我的脚本有菜单和子菜单 像这样
是的 什么东西当我选择数字1并按回车键时 将出现子菜单
是的 什么东西我如何能够做到这一点以及按下99后我的大问题怎么回到菜单
答案 0 :(得分:0)
试试这个示例脚本:我使用select循环菜单(1和2)。注意:bash中的select循环按顺序将bullet #s分配给它的选项,所以如果你需要子弹#echo为99,那么你必须想出一些东西(一个很好的家庭作业)或者你可以创建最后一个menu_2变量中的值为“.. .. 99_back_to_menu_1”。
对于内部选择menu_2循环,我也使用直到循环(仅当用户选择:back_to_menu_1选项时才退出内部选择循环)直到用户选择menu_2中的特定选项,他将再次被提示输入menu_2选项n再次。
如果你愿意,可以注释掉直到循环(第#,18,19和31行)。仅当用户选择“退出”退出时,Menu_1才会退出。
#!/bin/bash
## Lets say you have all your inputs for your menu_1 in file menu1 and menu_2 in menu2
## if input data is in a file, then do: menu1="$(cat menu_1)" and same for menu_2 with cat menu_2 file OR you can set menu_1 or menu_2 values (separated by spaces): menu_1="giga koba fifa"; menu_2="shenzi ed"
## lets say you have.
menu_1="Menu_1_a Menu_1_b Menu_1_c exit"
menu_2="Menu_2_i Menu_2_ii Menu_2_iii Menu_2_iv back_to_menu1"
select m1 in ${menu_1};
do
if [[ -n ${m1} ]]; then
echo -e "\n- Menu1: You selected: \"${m1}\"\n"
if [[ "$m1" == "exit" ]]; then break; fi ## break from outer select loop only if user selects "exit" to exit the outer select loop.
##--------Inner/Second select loop------------- start
until [[ "$m2" == "back_to_menu1" ]];
do
select m2 in ${menu_2};
do
if [[ -n ${m2} ]]; then
echo -e "\n- Menu2: You selected: \"${m2}\" --- OK, do something here for this selection in menu2\n"
echo -e "\\n\n\t -- OK, I did something for $m2 option .... !! nice\n\n\n";
if [[ "$m2" == "back_to_menu1" ]]; then break; fi ## break from outer select loop only if user selects "exit" to exit the outer select loop.
break; ## break from inner select loop as soon as user select any value.
else
echo -e "\n- Invalid selection \"${REPLY}\"...Please try again.\n\n";
fi
done ## done inner select loop
done ## done for until loop
##--------Inner/Second select loop------------- close
else
echo -e "\n- Invalid selection \"${REPLY}\"...Please try again.\n\n";
fi
done