shell腳本(三)
公司主營業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出石棉免費(fèi)做網(wǎng)站回饋大家。
一.使用if條件語句
案例:
單分支IF
1).根分區(qū)已用空間>80%則報(bào)警,否則不執(zhí)行任何操作
#!/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).執(zhí)行腳本時(shí)指定IP,結(jié)果為ping該主機(jī),若通,則顯示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
練習(xí)
#!/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是否已經(jīng)啟動(dòng),若已啟動(dòng),則提示已運(yùn)行,否則啟動(dòng)httpd服務(wù)
systemctl status httpd
if [ $? -eq 0 ]
then
echo “Service httpd is running.”
else
Systemctl start httpd
fi
雙分支IF
4).判斷/tmp目錄下是否有win目錄,若不存在則創(chuàng)建目錄,存在不執(zhí)行任何操作
#!/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已經(jīng)存在了。"
fi
多分支示例:
腳本可互動(dòng)輸入分?jǐn)?shù),并判斷分?jǐn)?shù)在90-100之間,判斷為優(yōu)秀,60-89之間為合格,59-40以下其他為不及格努力;39以下復(fù)讀,其它輸入為非法輸入。
#!/bin/bash
echo "=======Test========"
read -p "請輸入你的成績" Source
if [ $Source -gt 100 ] ; then
echo "輸入錯(cuò)誤"
elif [ $Source -lt 0 ] ; then
echo "輸入錯(cuò)誤"
elif [ $Source -ge 90 ] ; then
echo "優(yōu)秀"
elif [ $Source -ge 60 ] ; then
echo "及格"
elif [ $Source -ge 40 ] ; then
echo "努力"
else
echo "復(fù)讀";
fi
二、循環(huán)語句:
1、FOR循環(huán)寫法
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循環(huán)寫法
如:
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 "輸入錯(cuò)誤"
elif [ $Source -lt 0 ] ; then
echo "輸入錯(cuò)誤"
elif [ $Source -ge 90 ] ; then
echo "優(yōu)秀"
elif [ $Source -ge 60 ] ; then
echo "及格"
elif [ $Source -ge 40 ] ; then
echo "努力"
elif [ $Source -ge 0 ] ; then
echo "復(fù)讀";
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 "輸入錯(cuò)誤"
elif [ $Source -lt 0 ] ; then
echo "輸入錯(cuò)誤"
elif [ $Source -ge 90 ] ; then
echo "優(yōu)秀"
elif [ $Source -ge 60 ] ; then
echo "及格"
elif [ $Source -ge 40 ] ; then
echo "努力"
elif [ $Source -ge 0 ] ; then
echo "復(fù)讀";
else
echo "input number error."
fi
else
echo "error,not number."
fi
fi
done
補(bǔ)充
循環(huán)結(jié)構(gòu)中數(shù)值增量需要與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
作業(yè):用腳本完成
1.公司要求新進(jìn)一批實(shí)習(xí)生,要為他們建立用戶名shixi01 ---- shixi10,要求所有人的密碼初始都為great123。
2.用FOR循環(huán)寫九九乘法表。
3.用for及while結(jié)構(gòu)分別完成掃描本網(wǎng)段址的腳本