第二十三章 SHELL腳本-CENTOS7.5知識


shell腳本(三)

. 使用if條件語句

clip_image002

案例:

單分支IF

1).根分區已用空間>80%則報警,否則不執行任何操作

#!/bin/bash

echo "===Check disk usage...==="

DISKU=df -h|awk '/vda1/{print $5}'|awk -F% '{print $1}'

if [ $DISKU -gt 10 ] ; then

echo "warning,Disk vda1 is over 90 usages....."

echo "warning,Disk vda1 is over 90 usages....."> /var/log/diskusage.log

else

echo "disk vda1 is normal.Good luck."

fi


2).執行腳本時指定IP,結果爲ping該主機,若通,則顯示up,否則顯示down

#!/bin/bash

ping -c2 172.18.199.10  &>> /dev/null

if [ $? -eq 0 ]

then

echo "The IP $1 is up."

else

echo "The IP $1 is down."

fi


練習

#!/bin/bash

#writed by jason.check ip.

clear

echo ===Check IP up or down===

read -p "Please input your IP address:" CKIP

if [[ "$CKIP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ; then

        echo "nice ip $CKIP"

        ping -c1 -w 1 $CKIP &> /dev/null

                if [  $? -eq 0 ] ; then

                        echo "The ip $CKIP is up."

                else

                        echo "The ip $CKIP is down."

                fi

else

        echo "wrong ip $CKIP."

        echo "error,input wrong."

fi




3).判斷httpd是否已經啓動,若已啓動,則提示已運行,否則啓動httpd服務

systemctl status httpd

if [ $? -eq 0 ]

then

echo “Service httpd is running.”

else

Systemctl start  httpd

fi



雙分支IF

4).判斷/tmp目錄下是否有win目錄,若不存在則創建目錄,存在不執行任何操作

#!/bin/bash

if [ ! -e /tmp/win ] ;then

echo "To create dir win"

mkdir /tmp/win

elif [ -f /tmp/win ] ;then

echo "To del win file"

rm -rf /tmp/win

echo "To create dir win"

mkdir /tmp/win

else

echo "此目錄win已經存在了。"

fi


多分支示例:

腳本可互動輸入分數,並判斷分數在90-100之間,判斷爲優秀,60-89之間爲合格,59-40以下其他爲不及格努力;39以下復讀,其它輸入爲非法輸入。

#!/bin/bash

echo "=======Test========"

read -p "請輸入你的成績" Source

if [ $Source -gt 100 ] ; then

echo "輸入錯誤"

elif [ $Source -lt 0 ] ; then

echo "輸入錯誤"

elif [ $Source -ge 90 ] ; then

echo "優秀"

elif [ $Source -ge 60 ] ; then

echo "及格"

elif [ $Source -ge 40 ] ; then

echo "努力"

else

echo "復讀";

fi


二、循環語句:

clip_image004

clip_image006

1FOR循環寫法

for((i=1;i<=10;i++));

for i in `seq 10`

for i in {1..10}


#!/bin/bash

for i in {1..10}

do

echo “$i”

done

#!/bin/bash

for i in $*

do

echo “$i”

done

for  i in {30..39}

do

echo $i

done


vim num.txt

#!/bin/bash

for i in `cat num.txt`

do

echo “$i”

done


#!/bin/bash

ipnet='172.18.11.'

for IPaddress in {1..254}

do

ping -c 1 $ipnet$IPaddress &> /dev/null

if [ $? -eq 0 ] ; then

echo "This host $ipnet$IPaddress is up."

echo "This host $ipnet$IPaddress is up." >> /tmp/ping-18net.log

else

echo "This host $ipnet$IPaddress is down."

fi

done

echo "Net18 ping over."


#!/bin/bash

for (( i = 1; i <=9; i++ ))

do

for (( j=1; j <= i; j++ ))

do

let "chengji = i * j"

echo -n "$i*$j=$chengji "

done

echo ""

done


2while循環寫法

clip_image008

如:

i=1

while [ $i -lt 10 ]

do

echo $i

let i++

done



#!/bin/bash

echo "=======Test score========"

while true ;

do

read -p "請輸入你的成績: " Source

if [ "$Source" = "exit" ] ; then

    exit 

elif [[ "$Source" != [0-9]* ]] ; then

echo "err,you input character."

elif [ $Source -gt 100 ] ; then

echo "輸入錯誤"

elif [ $Source -lt 0 ] ; then

echo "輸入錯誤"

elif [ $Source -ge 90 ] ; then

echo "優秀"

elif [ $Source -ge 60 ] ; then

echo "及格"

elif [ $Source -ge 40 ] ; then

echo "努力"

elif [ $Source -ge 0 ] ; then

echo "復讀";

else

echo "input error."

fi

done




補充

循環結構中數值增量需要與let合作,如let i++

i++ 等同於 i=i+1

i+=1 等同於 i=i+1

i+=2 i=i+2

i--  i=i-1

i-=2 i=i-2

i*=2 i=i*2


作業:用腳本完成

1.公司要求新進一批實習生,要爲他們建立用戶名shixi01 ---- shixi10,要求所有人的密碼初始都爲great123。

2.用FOR循環寫九九乘法表。

3.用for及while結構分別完成掃描本網段址的腳本

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章