linux - 菜单和子菜单,然后返回到Linux上的菜单脚本

标签 linux bash loops menu

嗨,我需要一些帮助,我在bash脚本中编写脚本,我的脚本具有“菜单”和“子菜单”
像这样

1.做点什么
2.做点什么
3.做点什么

当我选择数字1并按Enter键
将出现子菜单

1.做点什么
2.做点什么
99.返回菜单

我该怎么做以及按99时遇到的最大问题是如何返回菜单

最佳答案

试试这个示例脚本:我对菜单(1和2)使用了select循环。注意:bash中的select循环按顺序将项目符号#分配给其选项,因此,如果您需要将bullet#echo设置为99,则必须要解决(适合您的一项家庭作业),也可以创建最后一个menu_2变量中的值是“ .. .. 99_back_to_menu_1”。

对于内部选择菜单_2循环,我也使用了直到循环(仅当用户选择:back_to_menu_1选项时才退出内部选择循环),直到用户在menu_2中选择该特定选项,他才会再次被提示输入menu_2选项。

您可以注释掉直到循环(行号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

关于linux - 菜单和子菜单,然后返回到Linux上的菜单脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743462/

相关文章:

linux - 为什么我不能使用 int 0x80 从指向堆栈内存的指针进行 sys_write?

C++:指针、循环变量和结构的问题

python - 如何让 while 循环识别它是文件结尾

php - 如何获取沿圆周的像素

linux - RT Linux 内核是整体内核还是微内核(如 QNX)?

c - linux rpc命令和重传

c# - 从应用程序构建操作系统

bash - 如何使用-e检查bash中的错误

macos - 在 OSX 上使用 scp 完成远程路径

python - 在 Windows 10 中安装和运行 Auto-Sklearn