2013-10-18 2 views
1

사례문을 사용하여 메인 메뉴로 스크립트를 작성했습니다. 스크립트가 실행되면 환영 화면 정보가 화면에 표시되고 사용자가 시작 화면 항목을 비우기 위해 입력하면 사용자가 메뉴 항목에 해당하는 번호를 입력하라는 메시지가 표시됩니다 (menuNum 읽기). 그것은 잘 작동하지만 기능을 조금 확장하고 사용자가 시작 화면을 건너 뛰고 스크립트를 실행할 때 인수를 사용하여 메뉴 항목으로 바로 가길 원합니다.bash, 스크립트의 일부를 건너 뛰기위한 쉘 스크립트 인수

예를 들어 내 메뉴가 : 1) 파일 조작 2) 사용자 정보 3) 프로세스 인 경우 사용자가 콘솔에 "scriptfile.sh file"을 입력하여 파일 메뉴로 직접 이동하게하십시오. 이것은 어떻게 든 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}에서 매개 변수를 받아 들여야한다면'[[ "$ 1"= "file"]]; 다음 bash tsharonfp.sh 1; 출구; fi'는'tsharonfp.sh <1; '이 아니다. –

+0

1.'[['안에 변수를 인용 할 필요는 없으며 값에 공백이있는 경우에도'['내부에서만. 리터럴이 공백을 포함하고있을 때만 리터럴을 인용하면됩니다. 2.'exec bash tsharonfp.sh "를 $ 1"fi "라고 생각할 수도 있습니다. 'exec'를 사용한다는 것은'exec'가 실패하지 않는 한'exit' 할 필요가 없다는 것을 의미합니다. 3. 아마도 스크립트를 모듈화하는 것이 가장 좋습니다. – cdarke

답변

1

그냥 몇 가지 변경 사항이 문제를 돌봐 :

기본적으로,이 내 스크립트입니다. 인수를 기준으로 menuNum을 설정하고 설정되지 않은 경우 ask/read 만합니다.

#!/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'은 "파일"과 "사용자"와 "정보"등이 될 수 없습니다.'-'와'['를 사용하면 모호해질 수 있습니다.'[['''||' 명확 해? 나중에 스크립트에서 비슷한 것을 사용합니다! – cdarke

+0

@cdarke, 좋은 의견입니다! – svante

관련 문제