for語句
for loop in 1 2 3 4 5 do echo $loop done for loop in `seq 1 100` do echo $loop done for loop in `ls /tmp` do echo $loop done
while語句
while true do read -p "請輸入你的密碼:" passwd if [ $passwd = "aixocm" ] then echo "密碼輸入正確,welcome" break else echo "密碼輸入錯誤" continue fi done
until語句
use=`df -lh | sed -n '/\/$/{p}' | awk '{print $5}' |sed 's/%//g'` #如果/使用率滿足小於80%的條件,則不執行迴圈, #反之若/大於80%(即不滿足條件),則執行迴圈 until [ $use -lt 80 ] do echo "warning:your / user 80%" #向使用者提出警告,你的/使用率已經大於或等於80% exit 1 done #而是執行迴圈外的這條, #告訴使用者,你的/使用率是多少,還少於80% echo "now your / use $use%,less then 80%"
case語句
function directory() { [ -d $1 ] if [ $? -eq 0 ] then echo "$1存在" else echo "$1不存在" fi } tput bold --->#加粗 echo "===查詢選單===" tput sgr0 echo "1、查詢/opt/aa 目錄是否存在?" echo "2、查詢/opt/cc 目錄是否存在?" echo "3、查詢/opt/dd 目錄是否存在?" read -p "你想查詢啥?:" n case $n in 1) directory /opt/aa ;; 2) directory /opt/cc ;; 3) directory /opt/dd ;; *) echo error ;; esac
if語句
#if/else結構
if expression then command else command fi
#if/elif/else結構
if expression1
then command
elif expression2
then command
elif expression3
then command
else command
fi
迴圈控制符:break和continue
break:忽略迴圈體中任何語句和條件的限制,強制退出當前迴圈
continue:跳過continue後面的語句,執行下一次迴圈,直到條件為真