1:case語法
case string1 in
str1 ) commands1;;
str2 ) commands2;;
*) commands3;;
esac
這裏需要注意每個匹配的選項裏,執行相應的命令,直到;;結束。
2:爲了加深理解,做幾個練習。
設計一個腳本,實現自由選擇,按1,輸出當前CPU,內存使用情況;按2,輸出磁盤使用情況;按3,輸出當前建立的網絡連接。
#!/bin/bash
echo "------------------
1:show cpu and memory;
2:show disk usage;
3:show network;
------------------"
echo -n "please input a digits:"
read n
case $n in
1)top;;
2)df -h;;
3)netstat -and;;
*)echo $n ,input error!
exit 1;;
esac
執行下:
3:select語法
select var in ... ;
do
break
done
練習:提供一個選擇,你喜歡什麼動物?狗,貓,鳥,蜜蜂。
#!/bin/bash
echo "what animal do you like?"
select var in "dog" "cat" "bird" "bee"
do
break
done
echo "you like $var,very good!"
執行: