shell編程——select語句

轉載自:http://blog.sina.com.cn/s/blog_6151984a0100el04.html



select 表達式是一種bash的擴展應用,動作包括:
自動用1,2,3,4列出菜單 (沒有echo指令,自動顯示菜單
自動read輸入選擇 (沒有 read指令,自動輸入
賦值給變量 沒有賦值指令,自動輸入數字後,賦值字符串給變量

select典型例子

[macg@machome ~]$ vi test.sh

echo "What is your favourite OS?"

select var in "Linux" "Gnu Hurd" "Free BSD" "Other";
do
break select本身就是一個循環,break是當選擇後,就跳出循環
done

echo "You have selected $var"
[macg@machome ~]$ sh test.sh

What is your favourite OS?

1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 2

You have selected Gnu HurdSelect輸入選擇是數字,但變量值卻是字符串


雖然select本身就是循環,但不建議用他的循環 ,因爲select雖然循環卻不再顯示菜單,只循環輸入所以seleckt 語句乾脆直接用break,只執行一次,在其上另配while循環
while echo "display current netconfig:"
do

select vi in "ifconfig -a" "hosts" "netmasks" "quit"
do

case $vi in 雖然輸入是1,2,3,4,5,但case變量輸入值卻是菜單項(字符串)
"ifconfig -a") /sbin/ifconfig -a;;
"hosts") more hosts;;
"netmasks") more netmasks;;
"quit") return 0;;
*) continue;;
esac

break
done

done
$ sh test
display current netconfig
1) ifconfig -a 3) netmasks
2) hosts 4) quit


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章