1.單分支if條件語句
if [ 條件判斷式 ] ; then
程式 fi
#或者if [ 條件判斷式 ]
then
程式 fi 單分支條件語句需要注意幾個點
● if語句使用fi結尾,和一般語言使用大括號結尾不同
● [ 條件判斷式 ]就是使用test命令判斷,所以中括號和條件判斷式之間必須有空格
● then後面跟符合條件之後執行的程式,可以放在之後,用「;」分割。也可以換行寫入,就不需要「;」了
例:#!/bin/bash
# 統計根分割槽使用率
rate=$(df -h | grep "/dev/sda3" | awk '' | cut -d "%" -f 1)
# 把根分割槽使用率作為變數值賦予變數rate
if [ $rate -ge 80 ]
then
echo "warning! /dev/sda3 is full!"
fi
2.雙分支if條件語句
if [ 條件判斷式 ]
then
條件成立時,執行的程式
else
條件不成立時,執行的另乙個程式
fi例1:備份mysql資料庫
#!/bin/bash
#備份mysql資料庫。
ntpdateasia.pool.ntp.org&>/dev/null
#同步系統時間
date=$(date+%y%m%d)
#把當前系統時間按照「年月日」格式賦予變數
datesize=$(du-sh/var/lib/mysql)
#統計mysql資料庫的大小,並把大小賦予size變數
if [ -d/tmp/dbbak ]
then
echo"date:$date!" > /tmp/dbbak/dbinfo.txt
echo"datasize$size">>/tmp/dbbak/dbinfo.txt
cd/tmp/dbbak
tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &>/dev/null
rm -rf /tmp/dbbak/dbinfo.txt
else
mkdir /tmp/dbbak
echo"date:date!">/tmp/dbbak/dbinfo.txt
echo"datasize:size">>/tmp/dbbak/dbinfo.txt
cd/tmp/dbbak
tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql
dbinfo.txt &> /dev/null
rm -rf /tmp/dbbak/dbinfo.txt
fi例子2:判斷apache是否啟動
#使用nmap命令掃瞄伺服器,並擷取apache服務的狀態,賦予變數port
fi
3.多分支if條件語句
if [ 條件判斷式1 ]
then
當條件判斷式1成立時,執行程式1
elif [ 條件判斷式2 ]
then
當條件判斷式2成立時,執行程式2
……省略更多條件…
else
當所有條件都不成立時,最後執行此程式
fi例:
#!/bin/bash
#判斷使用者輸入的是什麼檔案
read-p"please in put a filename:"file #接收鍵盤的輸入,並賦予變數file
if [ -z "$file" ] #判斷file變數是否為空
then
echo "error,please input a filename"
exit 1
elif [ ! -e "$file" ] #判斷file的值是否存在
then
echo "your input is not a file!"
exit 2
elif [ -f "$file" ] #判斷file的值是否為普通檔案
then
echo "$file is a regulare file!"
elif [ -d "$file" ] #判斷file的值是否為目錄檔案
then
echo "$file is a directory!"
else
echo"$file is another file!"
fi
多分支case條件語句
● case語句和if…elif…else語句一樣都是多分支條件語句,不過和if多分支條件語句不同的是,case語句只能判斷一種條件關係,而if語句可以判斷多種條件關係。
case $ 變數名 in
" 值1")
如果變數的值等於值1,則執行程式1
; ;" 值2")
如果變數的值等於值2,則執行程式2
; ;
…省略其他分支…
* )
如果變數的值都不是以上的值,則執行此程式
; ;esac
例:#!/bin/bash
#判斷使用者輸入
read -p "please choose yes/no:" -t 30 cho
case $cho in
"yes")
echo"your choose is yes!"
;;
"no")
echo"your choose is no!"
;;*) echo"your choose is error!"
;;esac
語法一(適合批量解壓縮、批量新增使用者等)
for 變數 in 值1 值2 值3
do 程式
done
例:#!/bin/bash
for city in beijing nanjing shanghai
do echo "this city is $city "
done
輸出:this city is beijing
this city is nanjing
this city is shanghai
語法二
for((初始值;迴圈控制條件;變數變化))
do 程式
done
例:#!/bin/bash
#從1加到100
num=0
for((i=1;i<=100;i++))
do num=$(($num+$i))
done
echo "this sum of 1+2+...+100 is: $num"
1.while迴圈
● while迴圈是不定迴圈,也稱作條件迴圈。只要條件判斷式成立,迴圈就會一直繼續,直到條件判斷式不成立,迴圈才會停止。這就和for的固定迴圈不太一樣了
while [ 條件判斷式 ]
do 程式
done
例:#!/bin/bash
#從1加到100
i=1s=0
while [ $i -le 100 ] #如果變數i的值小於等於100,則執行迴圈
do s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "the sum is: $s"
2、 until迴圈
● until迴圈,和while迴圈相反,until迴圈時只要條件判斷式不成立則進行迴圈,並執行迴圈程式。一旦迴圈條件成立,則終止迴圈。
until [ 條件判斷式 ]
do 程式
done
例:#!/bin/bash
#從1加到100
i=1s=0
until [ $i -gt 100 ] #迴圈直到變數i的值大於100,就停止迴圈
do s=$(( $s+$i ))
i=$(( $i+1 ))
done
echo "the sum is: $s"
4 shell程式設計之流程控制
if case for while 1 if判斷 if 條件判斷式 then 程式fi 或者if 條件判斷式 then 程式 elif 條件判斷式 程式 fi 注意事項 1 條件判斷式中,中括號和判斷式執行必須有空格 2 推薦使用第二種方式 編寫shell程式,如果輸入的引數大於等於60,則輸出 及...
Shell學習筆記 流程控制
分支兩種 if 和 case 迴圈三種 for until while.1.if if then elif then else fi 沒啥說的和c的差不多.就是別忘了 then.2.case case var in a b c esac 和switch的意思差不多,這個多了 表示或的關係,感覺更靈活...
shell程式設計(條件判斷與流程控制)學習筆記
流程控制語句 一 條件判斷式 二 單分支if語句 三 雙分支if語句 四 多分支if語句 五 case語句 六 for迴圈 七 while迴圈和until迴圈 一 條件判斷式 1 按照檔案型別進行判斷 2 按照檔案許可權進行判斷 3 兩個檔案之間進行比較 4 兩個整數之間比較 5 字串的判斷 6 多...