问题描述
我创建了一个简单的bash脚本,我想在其中创建一个选择菜单,如下所示:
$./script
echo "Choose your option:"
1) Option 1
2) Option 2
3) Option 3
4) Quit
根据用户的选择,我希望执行不同的操作。我是一个bash shell脚本noob,我在网上搜索了一些答案,但没有真正具体的。
最佳解决方案
#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice 3"
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
在需要select
循环退出的地方添加break
语句。如果未执行break
,则select
语句循环,菜单为re-displayed。
次佳解决方案
本身并不是一个新的答案,但由于目前还没有被接受的答案,所以这里有一些编码技巧和技巧,对于选择和zenity:
title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")
echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
while opt=$(zenity --title="$title" --text="$prompt" --list \
--column="Options" "${options[@]}"); do
case "$opt" in
"${options[0]}" ) zenity --info --text="You picked $opt, option 1";;
"${options[1]}" ) zenity --info --text="You picked $opt, option 2";;
"${options[2]}" ) zenity --info --text="You picked $opt, option 3";;
*) zenity --error --text="Invalid option. Try another one.";;
esac
done
值得一提:
-
两者都会循环直到用户明确选择退出(或取消退出)。对于交互式脚本菜单来说,这是一个很好的方法:在选择了选择并执行操作之后,再次显示菜单以供选择。如果选择仅用于one-time,则只需在
esac
后使用break
(可以进一步减小zenity方法) -
case
都是index-based,而不是value-based。我认为这是更容易编码和维护 -
阵列也用于
zenity
方法。 -
“Quit”选项不在最初的原始选项中。在需要时它是”added”,所以你的阵列保持干净。毕竟,无论如何”Quit”不需要,用户只需点击”Cancel”(或关闭窗口)退出。注意两者如何使用相同的,未触及的选项数组。
-
PS3
和REPLY
变量不能重命名。select
被硬编码使用这些。脚本中的所有其他变量(选项,选项,提示,标题)都可以具有您想要的任何名称,只要您进行调整即可
第三种解决方案
使用dialog
,该命令如下所示:
dialog --clear --backtitle "Backtitle here" --title "Title here" --menu "Choose one of the following options:" 15 40 4 \
1 "Option 1" \
2 "Option 2" \
3 "Option 3"
把它放在脚本中:
#!/bin/bash
HEIGHT=15
WIDTH=40
CHOICE_HEIGHT=4
BACKTITLE="Backtitle here"
TITLE="Title here"
MENU="Choose one of the following options:"
OPTIONS=(1 "Option 1"
2 "Option 2"
3 "Option 3")
CHOICE=$(dialog --clear \
--backtitle "$BACKTITLE" \
--title "$TITLE" \
--menu "$MENU" \
$HEIGHT $WIDTH $CHOICE_HEIGHT \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
clear
case $CHOICE in
1)
echo "You chose Option 1"
;;
2)
echo "You chose Option 2"
;;
3)
echo "You chose Option 3"
;;
esac
第四种方案
你可以使用这个简单的脚本来创建选项
#!/bin/bash
echo "select the operation ************ 1)operation 1 2)operation 2 3)operation 3 4)operation 4 "
read n
case $n in
1) commands for opn 1;;
2) commands for opn 2;;
3) commands for opn 3;;
4) commands for opn 4;;
*) invalid option;;
esac
第五种方案
由于这是针对Ubuntu的,你应该使用任何后端debconf配置来使用。您可以通过以下方式找到debconf后端:
sudo -s "echo get debconf/frontend | debconf-communicate"
如果它说”dialog”那么它可能使用whiptail
或dialog
。在Lucid上它是whiptail
。
如果失败,按照Dennis Williamson的解释使用bash “select”。
第六种方案
我使用了Zenity,它在Ubuntu中似乎一直存在,运行得非常好,并且具有很多功能。这是可能菜单的草图:
#! /bin/bash
selection=$(zenity --list "Option 1" "Option 2" "Option 3" --column="" --text="Text above column(s)" --title="My menu")
case "$selection" in
"Option 1")zenity --info --text="Do something here for No1";;
"Option 2")zenity --info --text="Do something here for No2";;
"Option 3")zenity --info --text="Do something here for No3";;
esac
第七种方案
#!/bin/sh
show_menu(){
NORMAL=`echo "\033[m"`
MENU=`echo "\033[36m"` #Blue
NUMBER=`echo "\033[33m"` #yellow
FGRED=`echo "\033[41m"`
RED_TEXT=`echo "\033[31m"`
ENTER_LINE=`echo "\033[33m"`
echo -e "${MENU}*********************************************${NORMAL}"
echo -e "${MENU}**${NUMBER} 1)${MENU} Mount dropbox ${NORMAL}"
echo -e "${MENU}**${NUMBER} 2)${MENU} Mount USB 500 Gig Drive ${NORMAL}"
echo -e "${MENU}**${NUMBER} 3)${MENU} Restart Apache ${NORMAL}"
echo -e "${MENU}**${NUMBER} 4)${MENU} ssh Frost TomCat Server ${NORMAL}"
echo -e "${MENU}**${NUMBER} 5)${MENU} ${NORMAL}"
echo -e "${MENU}*********************************************${NORMAL}"
echo -e "${ENTER_LINE}Please enter a menu option and enter or ${RED_TEXT}enter to exit. ${NORMAL}"
read opt
}
function option_picked() {
COLOR='\033[01;31m' # bold red
RESET='\033[00;00m' # normal white
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e "${COLOR}${MESSAGE}${RESET}"
}
clear
show_menu
while [ opt != '' ]
do
if [[ $opt = "" ]]; then
exit;
else
case $opt in
1) clear;
option_picked "Option 1 Picked";
sudo mount /dev/sdh1 /mnt/DropBox/; #The 3 terabyte
menu;
;;
2) clear;
option_picked "Option 2 Picked";
sudo mount /dev/sdi1 /mnt/usbDrive; #The 500 gig drive
menu;
;;
3) clear;
option_picked "Option 3 Picked";
sudo service apache2 restart;
show_menu;
;;
4) clear;
option_picked "Option 4 Picked";
ssh lmesser@ -p 2010;
show_menu;
;;
x)exit;
;;
\n)exit;
;;
*)clear;
option_picked "Pick an option from the menu";
show_menu;
;;
esac
fi
done
第八种方案
我还有一个选项是这些答案的混合体,但是它的好处在于,您只需按一个键,然后脚本继续感谢-n
阅读选项。在这个例子中,我们提示关闭,重新启动,或者简单地使用ANS
作为我们的变量退出scrit,用户只需按下E,R或S.我还设置了缺省值退出,所以如果输入被按下,那么脚本将退出。
read -n 1 -p "Would you like to exit, reboot, or shutdown? (E/r/s) " ans;
case $ans in
r|R)
sudo reboot;;
s|S)
sudo poweroff;;
*)
exit;;
esac
第九种方案
serverfault已经回答了同样的问题。那里的解决方案使用whiptail。
第十种方案
打击花式菜单
先尝试一下,然后访问我的页面以获得详细的描述…不需要外部库或程序,如对话框或zenity …
#/bin/bash
# by oToGamez
# www.pro-toolz.net
E='echo -e';e='echo -en';trap "R;exit" 2
ESC=$( $e "\e")
TPUT(){ $e "\e[${1};${2}H";}
CLEAR(){ $e "\ec";}
CIVIS(){ $e "\e[?25l";}
DRAW(){ $e "\e%@\e(0";}
WRITE(){ $e "\e(B";}
MARK(){ $e "\e[7m";}
UNMARK(){ $e "\e[27m";}
R(){ CLEAR ;stty sane;$e "\ec\e[37;44m\e[J";};
HEAD(){ DRAW
for each in $(seq 1 13);do
$E " x x"
done
WRITE;MARK;TPUT 1 5
$E "BASH SELECTION MENU ";UNMARK;}
i=0; CLEAR; CIVIS;NULL=/dev/null
FOOT(){ MARK;TPUT 13 5
printf "ENTER - SELECT,NEXT ";UNMARK;}
ARROW(){ read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]];then echo up;fi
if [[ $key = $ESC[B ]];then echo dn;fi;}
M0(){ TPUT 4 20; $e "Login info";}
M1(){ TPUT 5 20; $e "Network";}
M2(){ TPUT 6 20; $e "Disk";}
M3(){ TPUT 7 20; $e "Routing";}
M4(){ TPUT 8 20; $e "Time";}
M5(){ TPUT 9 20; $e "ABOUT ";}
M6(){ TPUT 10 20; $e "EXIT ";}
LM=6
MENU(){ for each in $(seq 0 $LM);do M${each};done;}
POS(){ if [[ $cur == up ]];then ((i--));fi
if [[ $cur == dn ]];then ((i++));fi
if [[ $i -lt 0 ]];then i=$LM;fi
if [[ $i -gt $LM ]];then i=0;fi;}
REFRESH(){ after=$((i+1)); before=$((i-1))
if [[ $before -lt 0 ]];then before=$LM;fi
if [[ $after -gt $LM ]];then after=0;fi
if [[ $j -lt $i ]];then UNMARK;M$before;else UNMARK;M$after;fi
if [[ $after -eq 0 ]] || [ $before -eq $LM ];then
UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;}
INIT(){ R;HEAD;FOOT;MENU;}
SC(){ REFRESH;MARK;$S;$b;cur=`ARROW`;}
ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT
while [[ "$O" != " " ]]; do case $i in
0) S=M0;SC;if [[ $cur == "" ]];then R;$e "\n$(w )\n";ES;fi;;
1) S=M1;SC;if [[ $cur == "" ]];then R;$e "\n$(ifconfig )\n";ES;fi;;
2) S=M2;SC;if [[ $cur == "" ]];then R;$e "\n$(df -h )\n";ES;fi;;
3) S=M3;SC;if [[ $cur == "" ]];then R;$e "\n$(route -n )\n";ES;fi;;
4) S=M4;SC;if [[ $cur == "" ]];then R;$e "\n$(date )\n";ES;fi;;
5) S=M5;SC;if [[ $cur == "" ]];then R;$e "\n$($e by oTo)\n";ES;fi;;
6) S=M6;SC;if [[ $cur == "" ]];then R;exit 0;fi;;
esac;POS;done