2014-11-04 69 views
-1

您好,我需要一些幫助我的編程腳本在bash腳本我的劇本有菜單和子菜單 這樣菜單和子菜單,返回菜單腳本在Linux

1.do東西
2.do東西
3.do東西

當我選擇1號,然後按回車 會出現子菜單

1.do東西
2.do東西
99.back到菜單

我怎麼能做到這一點,我的大問題,當按99如何返回菜單

+0

你嘗試過什麼嗎?您是否搜索過在shell腳本中執行此類操作的方法? – 2014-11-04 19:35:26

+0

是的,我嘗試搜索,但我的問題回到菜單代碼 – ASKER 2014-11-04 19:36:40

+0

[這個問題](http://stackoverflow.com/q/26635101/258523)(和我的回答)有幫助嗎? – 2014-11-04 19:36:50

回答

0

試試這個示例腳本:我使用的菜單中選擇循環(1和2) 。注意:在bash中選擇循環將按照順序將子彈編號分配給它的選項,所以如果你需要將子彈編號回顯爲99,那麼這是你必須提出的事情(一個好的家庭作業)或者你可以創建最後一個menu_2變量中的值爲「.. .. 99_back_to_menu_1」。

對於內部選擇menu_2循環,我還使用直到循環(只有當用戶選擇back_to_menu_1選項時才退出內部選擇循環),直到用戶在menu_2中選擇該特定選項爲止,他將再次提示您輸入menu_2選項再次。

如果您想要(行#s,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