select variable in list
do # 循環(huán)開始的標(biāo)志
commands # 循環(huán)變量每取一次值,循環(huán)體就執(zhí)行一遍
done # 循環(huán)結(jié)束的標(biāo)志
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)十堰免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
select 循環(huán)主要用于創(chuàng)建菜單,按數(shù)字順序排列的菜單項(xiàng)將顯示在標(biāo)準(zhǔn)錯(cuò)誤上,等待用戶輸入
菜單項(xiàng)的間隔符由環(huán)境變量 IFS 決定
用于引導(dǎo)用戶輸入的提示信息存放在環(huán)境變量 PS3 中
用戶直接輸入回車將重新顯示菜單
與 for 循環(huán)類似,省略 in list 時(shí)等價(jià)于 in “$*”
用戶輸入菜單列表中的某個(gè)數(shù)字,執(zhí)行相應(yīng)的命令
用戶的輸入被保存在內(nèi)置變量 REPLY 中。
實(shí)例 1
#!/bin/bash
#
#IFS 是系統(tǒng)分隔符變量;未指定輸入?yún)?shù)變量,系統(tǒng)默認(rèn)把腳本后跟輸入的參數(shù)存放REPLY變量里
clear
PS3="What is your preferred OS?"
IFS='|'
OS="Linux|Gnu Hurd|FreeBSD|Mac OS X"
select s in $OS
do
case $REPLY in
1|2|3|4) echo "You selected $s" ;;
*) exit ;;
esac
done
執(zhí)行結(jié)果
1) Linux
2) Gnu Hurd
3) FreeBSD
4) Mac OS X
What is your preferred OS?1
You selected Linux
What is your preferred OS?4
You selected Mac OS X
What is your preferred OS?e
[root@localhost shell]#
實(shí)例 2
#!/bin/bash
#
#PS3 用戶自定義的提示信息
clear
PS3="What is your preferred scripting language?"select s in bash perl python ruby quit
do
case $s in
bash|perl|python|ruby)
echo "You selected $s"
;;
quit) exit ;;
*) echo "You selected error,retry " ;;
esac
執(zhí)行結(jié)果
1) bash
2) perl
3) python
4) ruby
5) quit
What is your preferred scripting language?1
You selected bash
What is your preferred scripting language?2
You selected perl
What is your preferred scripting language?5
[root@localhost shell]#
實(shí)例 3
#!/bin/bash
#
#命令包查看腳本
PS3="Select a program you want to execute: "
TOPLIST="wget telnet htop atop nettop iftop ftop"
clear
select prog in $TOPLIST quit
do
[[ $prog == quit ]] && exit
rpm -q $prog > /dev/null && echo "$prog installed" || echo "$prog is not installed"done
執(zhí)行結(jié)果
1) wget 3) htop 5) nettop 7) ftop
2) telnet 4) atop 6) iftop 8) quit
Select a program you want to execute: 1
wget installed
Select a program you want to execute: 8
[root@localhost shell]#