我有一个脚本,主菜单是使用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
答案 0 :(得分: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