shell學習之路:流程式控制制(if)

來源:互聯網
上載者:User

標籤:

1.單分支if條件陳述式

1 if [ 條件判斷式 ];then2     程式3 fi4 或者5 if [ 條件判斷式 ]6     then7         程式8 fi

注意事項:

1.if語句使用fi結尾,和一般語言使用大括弧結尾不同

2.[ 條件判斷式 ]就是使用test命令判斷,所以中括弧和條件判斷式之間必須有空格 前後都要有

3.then後面跟符合條件之後執行的程式,可以放在[]之後,用";"分號分割。也可以換行寫入,就不需要";"了

例如:判斷分區使用率

指令碼說明: 我的根分區是/dev/sdb5 我將 df -h 中的第五列的百分數字部分提取出來

賦給$rate 在用if判斷比較結果是否大於80 大於則成立 不大於則不成立 成立則輸出警告 不成立則不執行任何

當然我也可以將警告替換成其他 例如email資訊或者觸發另一個警告指令碼以及將警告資訊組建記錄檔等等!

#!/bin/bash#統計根分區使用率#Author: pat (Email:239@qq.com)rate=$(df -h | grep "/dev/sda5" | awk ‘{printf $5"\n"}‘ | cut -d "%" -f 1)#把根分區使用率作為變數值賦予變數rateif [ $rate -ge 80 ]    then        echo "WARNING! /dev/sda5 is full!!"fi

2.雙分支if條件陳述式

1 if [ 條件判斷式 ]2     then3         條件成立時,執行的程式4     else5         條件不成立時,執行的程式6 fi

例如1:備份mysql資料庫

 

 1 #!/bin/bash 2 #備份mysql資料庫 3 ntpdate asia.pool.ntp.org $>/dev/null 4 #同步系統時間 5 date=$(date +%y%m%d) 6 #把當前系統時間的年月日格式賦予變數date 7 size=$(du -sh /var/lib/mysql) 8 #統計mysql資料庫檔案的大小,並把大小賦給變數size 9 if [ -d /tmp/dbbak ]10     then11         echo "Date : $date!" > /tmp/dbbak/dbinfo.txt12         echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt13         cd /tmp/dbbak14         tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &>/dev/null15         rm -rf /tmp/dbbak/dbinfo.txt16     else17         mkdir /tmp/dbbak18         echo "Date : $date!" > /tmp/dbbak/dbinfo.txt19         echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt20         cd /tmp/dbbak21         tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &>/dev/null22         rm -rf /tmp/dbbak/dbinfo.txt23 fi

 

樣本二:判斷apache是否啟動:

需要安裝nmap命令 CentOS下執行:yum -y installl nmap

 

 1 #!/bin/bash 2  3 port=$(nmap -sT localhost | grep tcp | grep http | awk ‘{print $2}‘) 4 echo "$port" 5 ##使用nmap命令掃描伺服器,並截取apache服務的狀態,賦給變數port 6 if [ "$port" == "open" ] 7         then 8                 echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log 9         else10                 if [ -f "/etc/init.d/httpd" ] ; then11                         /etc/init.d/httpd start &>/dev/null12                         echo "$(date) restart httpd !!" >> /tmp/autostart-err.log13                 else14                         echo "error is not httpd server your is chack apache?"15                 fi16 fi
http.sh

 

3.多分支if條件陳述式

1 if [ 條件判斷1 ] ; then2     當前條件判斷式1成立時,執行的程式3 elif [ 條件判斷2 ] ; then4     當前條件判斷2成立時,執行的程式5 .......可寫多個6 else7     當所有條件均不成立時,執行的程式8 fi

例如:1

#!/bin/bashread -p "Please input -a filename:" file# 接收鍵盤的輸入,並賦給變數fileif [ -z "$file" ]# 判斷變數file是否為空白        then                echo "Error please input a filename"                exit 1elif [ ! -e "$file" ] ; then# 判斷file的值是否存在        echo "Your input is not a file!"        exit 2elif [ -f "$file" ] ; then        echo "is file"elif [ -d "$file" ] ; then        echo "is directory"else        echo "$file is an other file!"fi

 

shell學習之路:流程式控制制(if)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.