您好,登錄后才能下訂單哦!
shell腳本(三)
一. 使用if條件語句
案例:
單分支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
二、循環語句:
1、FOR循環寫法
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
2、while循環寫法
如:
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
或
#!/bin/bash
#writed by jason,fenshu.
clear
echo "====Check fenshu...===="
while true
do
read -p "input your score:" Source
expr $Source + 1 &>/dev/null
if [ ! $? -eq 0 ] ; then
echo "error,wrong.You must input number."
elif [ "$Source" -gt 100 ] ; then
echo "Number too big."
elif [ "$Source" -lt 0 ] ; then
echo "Number too small."
elif [ "$Source" -ge 90 ] ; then
echo "You are great."
elif [ "$Source" -ge 60 ] ; then
echo "You are just so so ."
elif [ "$Source" -ge 40 ] ; then
echo "No pass,try your best."
elif [ "$Source" -ge 0 ] ; then
echo "You must go home."
else
echo "You input err,try again."
fi
done
或者
#!/bin/bash
echo "=======Test score========"
while true ;
do
read -p "請輸入你的成績: " Source
if [ "$Source" = "exit" ] ; then
exit
else
if [[ "$Source" =~ ^[1-9][0-9]{0,2}$ ]] ; then
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 "努力"
elif [ $Source -ge 0 ] ; then
echo "復讀";
else
echo "input number error."
fi
else
echo "error,not number."
fi
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結構分別完成掃描本網段址的腳本
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。