shell腳本應用(3)--語法結構

 

  1. 判斷語句
  • 條件判斷
test expression
[ expression ]
  • 條件表達式中經常使用的判斷
  1. 數值-eq -ne -gt -lt -ge -le[equal not greater than less]
  2. 字符串=: != -z -n[長度zero nozero]
  3. 文件-e -r -w -x -s -d -f -c -b[exist read write execute size directory file char block]
  • 語句結構
  1. if [ $mark -le 80 ]
  2. then
  3. echo "Your Grade is Average."
  4. elif [ $mark -gt 80 -a $mark -le 90 ]
  5. echo "Your Grade is Good."
  6. else
  7. echo "Your Grade is Outstanding."
  8. fi
 
  1. case分支
  • 語句結構

 

  1. case $choice in
  2. 1) echo "choice1";;
  3. 2) echo "choice2";;
  4. *) echo "out of choice"

 

 
  1. 循環語句
  • for循環語句結構
  1. for val in vals
  2. do
  3. ...
  4. done
  • while循環語句結構
  1. i=0
  2. while [ $i -le 100 ]
  3. do
  4. ...
  5. ((i=$i+1))
  6. done
相關文章
相關標籤/搜索