bash Linux 上的菜单和子菜单脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15417404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:51:39  来源:igfitidea点击:

Menu and Sub Menu Scripting On Linux

linuxbashloopsmenu

提问by John Delaney

There is 2 things i'm not sure about which i have been trying to do for a while and i'm sorry for sounding stupid, but i'm not sure where to integrate the code so that when option A or B is pressed in the Sub Menus it says "Option A Selected" I'm also not sure how to loop the Sub Headings so that when it has said "Option A/B Selected" it goes back to the sub menu screen. Until i Press the back to main menu button when it will go to the Main Menu. I'd much appreciate any help as i am new to this and struggling with this bit in particular, Thanks!

有两件事我不确定我已经尝试做一段时间了,很抱歉听起来很愚蠢,但我不确定在哪里集成代码,以便在按下选项 A 或 B 时子菜单上显示“已选择选项 A” 我也不知道如何循环子标题,以便在显示“已选择选项 A/B”时返回子菜单屏幕。直到我按下返回主菜单按钮时,它才会进入主菜单。我非常感谢任何帮助,因为我是新手,特别是在这一点上挣扎,谢谢!

采纳答案by suspectus

One way of simplifying the task is to keep each menu in it's own function, Each menu loops until the user presses the exit key. In this case 'x' is used. My bash does not

简化任务的一种方法是将每个菜单保留在它自己的功能中,每个菜单循环直到用户按下退出键。在这种情况下,使用“x”。我的 bash 没有

function subopt1
{
   subopt1=""
   while [ "$subopt1" != "x" ]
   do
      echo Sub Menu 1 Heading
      echo Option A
      echo Option B
      echo x Back to Main Menu
      read -p "Select sub option1" subopt1
   done
 }

 function subopt2
 {
     subopt2=""
     while [ "$subopt2" != "x" ]
     do
         echo Sub Menu 2 Heading
         echo Option A
         echo Option B
         echo x Back to Main Menu
         read -p "Select sub-option2" subopt2    
     done
 }

 function mainopt
 {
    opt=""
    while [ "$opt" != "x" ]
    do
        echo Menu Heading
        echo Sub Menu 1
        echo Sub Menu 2
        read -p "Select Otion: " opt
        if [ "$opt" = "1" ]; then
             subopt1
        elif [ "$opt" = "2" ]; then
             subopt2
        elif [ "$opt" = "x" ];then
             break
        fi
   done
}

mainopt

回答by Gilles Quenot

Another approach :

另一种方法:

select x in submenu1 submenu2 exit ; do
    [[ $x == exit ]] && exit 0
    select y in optionA optionB; do
        echo "submenu $y heading"
        echo "$x selected"
        break
    done
done

Example

例子

1) submenu1
2) submenu2
3) exit
> 1
1) optionA
2) optionB
> 1
submenu optionA heading
submenu1 selected
>