select结构是建立菜单的另一种工具,该结构是从ksh中引入的
1.select格式
select variable [ in list ]
do
commands
done
如果忽略了in list列表,那么select命令将会使用传递到脚本的命令行参数($@),或者是函数参数(当select是在函数中时)
2.样例
样例-1:
[root@kibana ~]# cat select-1.sh
#!/bin/bashPS3='Choose your favorite vegetable: '
#设置提示符字符串
echoselect vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
doechoecho "Your favorite vegetable is $vegetable."break
doneexit 0
[root@kibana ~]# sh select-1.sh 1) beans
2) carrots
3) potatoes
4) onions
5) rutabagas
Choose your favorite vegetable: 1Your favorite vegetable is beans.
[root@kibana ~]#
样例-2:
[root@kibana ~]# cat select-2.sh
#!/bin/bashPS3='Choose your favorite vegetable: '
#设置提示符字符串
echofunction choice_of_vegetable ()
{select vegetable#[in list]被忽略,select会使用传递给函数的参数.doechoecho "Your favorite vegetable is $vegetable."breakdone
}choice_of_vegetable "beans" "carrots" "potatoes" "onions" "rutabagas"# $1 $2 $3 $4 $5#传递给choice_of_vegetable()的函数
exit 0
[root@kibana ~]# sh select-2.sh 1) beans
2) carrots
3) potatoes
4) onions
5) rutabagas
Choose your favorite vegetable: 1Your favorite vegetable is beans.
[root@kibana ~]#