2013-10-18 91 views
1

我有一個使用case語句完成主菜單的腳本。腳本運行,歡迎屏幕信息回顯至屏幕,用戶點擊進入以清除歡迎屏幕內容(空讀取語句),用戶被要求輸入與菜單項目相對應的編號(讀取菜單編號)。它可以正常工作,但我想稍微擴展功能,並允許用戶跳過歡迎屏幕,並在運行腳本時使用參數直接進入菜單項。bash,shell腳本參數跳過腳本的一部分

例如,如果我的菜單是:1)文件操作2)用戶信息3)進程,那麼我希望用戶在控制檯中鍵入「scriptfile.sh文件」直接進入文件菜單。這會以某種方式分配menuNum = 1。

我不知道這是可能的使用輸入重定向,或者真的如果可能的話。任何幫助或提示將不勝感激。謝謝。

基本上,這是我的腳本:

#!/bin/bash 
#DEFINE ARGUMENTS 
if [[ "$1" = "man" ]]; then man ./manpage.txt; exit; fi #argument "man" opens man page, closes main script 
if [ "$1" = "debug" ] 
    then clear 
    echo -e "This area provides debug information:\n" 
    echo -e "There's a lot here, but it's not related to my question." 
    echo -e "\nPress [Enter] to continue." 
    read 
fi 
if [[ "$1" = "file" ]]; then bash tsharonfp.sh < 1; exit; fi #go straight to file menu 
if [[ "$1" = "user" ]]; then bash tsharonfp.sh < 2; exit; fi #go straight to user menu 
if [[ "$1" = "info" ]]; then bash tsharonfp.sh < 3; exit; fi #go straight to info menu 
if [[ "$1" = "fun" ]]; then bash tsharonfp.sh < 4; exit; fi #go straight to fun menu 
if [[ "$1" = "process" ]]; then bash tsharonfp.sh < 5; exit; fi #go straight to proc menu 
#/DEFINE ARGUMENTS 
clear 
echo -e "My script title, contact info, and other stuff gets printed to screen here." 
read #hitting enter clears welcome screen stuff  
clear 
while : #opens while1 loop 
do #while1 loop 
echo -e "Main Menu\n" 
echo -e "[1] File Menu\n[2] User Menu\n[3] Info Menu\n[4] Fun Menu\n[5] Process Menu\n[88] Exit\n[99] Shutdown" 
echo -ne "\nEnter Choice: " 
read menuNum 

case $menuNum in #open mainmenu case 
    1) #File;; #statement isn't commented, of course, but you get the idea 
    2) #user;; 
    3) #info;; 
    4) #fun;; 
    5) #proc;; 
    88) exit;; 
    99) shutdown -h;; 
    *) echo "invalid input";; 
esaC#closes mainmenu case 
done #closes while1 loop 
+3

顯示您擁有的腳本。 – devnull

+0

我沒有看到問題tsharonfp.sh在做什麼?如果tsharonfp.sh必須接受{1,...,5,88,99}中的參數,則應該以這種方式傳遞它[if [[「$ 1」=「file」]];然後bash tsharonfp.sh 1;出口; fi' not with'tsharonfp.sh <1;' –

+0

1.您不需要在'[['中僅引用'['(即使這些值已經嵌入空格)'內部引用變量。你只需要在嵌入空格時引用文字。 2.你可以考慮'然後exec bash tsharonfp.sh「$ 1」fi「。使用'exec'意味着不需要'exit'(除非'exec'失敗)。 3.最好模塊化你的腳本。 – cdarke

回答

1

短短几年的變化需要你的問題的關心。根據參數設置menuNum,如果沒有設置,只詢問/閱讀。

#!/bin/bash 
#DEFINE ARGUMENTS 
if [[ "$1" = "man" ]]; then man ./manpage.txt; exit; fi #argument "man" opens man page, closes main script 
if [ "$1" = "debug" ] 
    then clear 
    echo -e "This area provides debug information:\n" 
    echo -e "There's a lot here, but it's not related to my question." 
    echo -e "\nPress [Enter] to continue." 
    read 
fi 


if [ "$1" = "file" -o "$1" = "1" ]; then menuNum=1; 
elif [ "$1" = "user" -o "$1" = "2" ]; then menuNum=2; 
elif [ "$1" = "info" -o "$1" = "3" ]; then menuNum=3; 
elif [ "$1" = "fun" -o "$1" = "4" ]; then menuNum=4; 
elif [ "$1" = "process" -o "$1" = "5" ]; then menuNum=5; 
else menuNum=0; fi 

#/DEFINE ARGUMENTS 
clear 
echo -e "My script title, contact info, and other stuff gets printed to screen here." 
[ $menuNum = 0 ] && read #hitting enter clears welcome screen stuff  

clear 
while : #opens while1 loop 
do #while1 loop 
echo -e "Main Menu\n" 
echo -e "[1] File Menu\n[2] User Menu\n[3] Info Menu\n[4] Fun Menu\n[5] Process Menu\n[88] Exit\n[99] Shutdown" 
echo -ne "\nEnter Choice: " 
[ $menuNum = 0 ] && read menuNum 

case $menuNum in #open mainmenu case 
    1) #File;; #statement isn't commented, of course, but you get the idea 
    2) #user;; 
    3) #info;; 
    4) #fun;; 
    5) #proc;; 
    88) exit;; 
    99) shutdown -h;; 
    *) echo "invalid input";; 
esaC#closes mainmenu case 
menuNum=0 
done #closes while1 loop 
+1

不會'elif'或'case'更好,而不是一堆if?畢竟,'$ 1'不能是「文件」和「用戶」和「信息」等。使用'['和'-o'可能是不明確的,也許使用'[[''和'||'可能是更清晰?無論如何,你在腳本中使用類似的東西! – cdarke

+0

@cdarke,好評! – svante