linux循环之select

select结构是建立菜单的另一种工具,该结构是从ksh中引入的

1.select格式

select variable [ in list ]

do

 commands

done

如果忽略了in list列表,那么select命令将会使用传递到脚本的命令行参数($@),或者是函数参数(当select是在函数中时)

2.样例

样例-1:

[root@kibana ~]# cat select-1.sh 
#!/bin/bash

PS3='Choose your favorite vegetable: '
#设置提示符字符串
echo

select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
  echo
  echo "Your favorite vegetable is $vegetable."
  break
done

exit 0
[root@kibana ~]# sh select-1.sh 

1) beans
2) carrots
3) potatoes
4) onions
5) rutabagas
Choose your favorite vegetable: 1

Your favorite vegetable is beans.
[root@kibana ~]# 

样例-2:

[root@kibana ~]# cat select-2.sh 
#!/bin/bash

PS3='Choose your favorite vegetable: '
#设置提示符字符串
echo

function choice_of_vegetable ()
{
  select vegetable
  #[in list]被忽略,select会使用传递给函数的参数.
  do
   echo
   echo "Your favorite vegetable is $vegetable."
   break
  done
}

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: 1

Your favorite vegetable is beans.
[root@kibana ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值