標籤:style blog io color ar sp div on art
一、文法
select 變數 in con1 con2 con3 # 自動列出 con1,con2,con3 的選擇菜單do #執行內容break # select本身就是一個迴圈,break是當選擇後,就跳出迴圈done
二、練習
- select + case 類比 Linux 啟動指令碼
function programstatus(){ if [ "$1" -eq 0 ]; then echo "* program is running" else echo "* program has stopped" fi}status=0 #0: start; 1:stopselect p in "start" "stop" "status" "restart" "*" ""docase "$p" in"start") status=0 programstatus $status ;;"stop") status=1 programstatus $status ;;"status") programstatus $status ;;"restart") if [ "$status" -eq 0 ]; then status=1 echo "* program has stopped" fi status=0 echo "* program is running" ;;"") break ;;*) echo "Plz input [start|stop|status|restart]" ;;esacdone
[email protected]:~$ ./test.sh1) start2) stop3) status4) restart5)#? 1* program is running#? 2* program has stopped#? 3* program has stopped#? 4* program is running#? 5[email protected]:~$
Shell 編程基礎之 Select 練習