1

我有一个使用 case 语句完成主菜单的脚本。脚本运行,欢迎屏幕信息回显到屏幕,用户按回车键清除欢迎屏幕内容(空的读取语句),要求用户输入与菜单项对应的数字(读取 menuNum)。它按原样工作正常,但我想稍微扩展功能并允许用户在运行脚本时通过使用参数跳过欢迎屏幕并直接进入菜单项。

例如,如果我的菜单是: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
4

1 回答 1

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
于 2013-10-18T10:43:35.253 回答