select variable in list
do # 循環開始的標誌
commands # 循環變量每取一次值,循環體就執行一遍
done # 循環結束的標誌python
select 循環主要用於建立菜單,按數字順序排列的菜單項將顯示在標準錯誤上,等待用戶輸入
菜單項的間隔符由環境變量 IFS 決定
用於引導用戶輸入的提示信息存放在環境變量 PS3 中
用戶直接輸入回車將從新顯示菜單
與 for 循環相似,省略 in list 時等價於 in 「$*」
用戶輸入菜單列表中的某個數字,執行相應的命令
用戶的輸入被保存在內置變量 REPLY 中。shell
實例 1ruby
#!/bin/bash
#
#IFS 是系統分隔符變量;未指定輸入參數變量,系統默認把腳本後跟輸入的參數存放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
donebash
執行結果ide
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]# ui
實例 2ip
#!/bin/bash
#
#PS3 用戶自定義的提示信息
clear
PS3="What is your preferred scripting language?"getselect 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 " ;;
esacit
執行結果class
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]#
實例 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
執行結果
1) wget 3) htop 5) nettop 7) ftop2) telnet 4) atop 6) iftop 8) quitSelect a program you want to execute: 1wget installedSelect a program you want to execute: 8[root@localhost shell]#