shell學習筆記(3)
阿新 • • 發佈:2018-03-16
shell 基礎 雜記if
一、if基礎
1、單分支
1.1 語法
if語句語法
單分支結構語法:
if [條件]; then
指令
fi
或
if [條件]
then
指令
fi
1.2 例子
[root@master4 day4]# cat if-single1.sh #!/bin/sh #功能:單分支if結構整數比較,用-lt格式例子 #created by xsz #date:20180107 if [ 10 -lt 12 ] then echo "Yes,10 is less than 12" fi [root@master4 day4]# sh if-single1.sh Yes,10 is less than 12 傳參方方式: [root@master4 day4]# cat if02.sh #/bin/sh if [ $1 -lt $2 ] then echo "Year,$1 is less than $2" fi read接收方式: [root@master4 day4]# cat if03.sh #/bin/sh read -t 10 -p "please input two number:" num1 num2 if [ $num1 -lt $num2 ] then echo "Year,$num1 is less than $num2" fi 以上腳本問題 1、無法完整比較整數大小 2、沒有對參數的個數以及變量內容做判斷
1.3 範例3
範例三(請思考):開發腳本實現如果/server/scripts下面存在if3.sh就輸出到屏幕。
註意:如果執行腳本後發現該if3.sh不存在,就自動創建這個if3.sh腳本。
我的腳本: #!/bin/bash i=/server/scripts/if3.sh [ -e $i ] && cat $i && exit 0 if [ $(sh $i 2> /dev/null;echo $?) != 0 ] then echo "$i is no exist,touching if03.sh..." echo "I am if03.sh" > $i fi 老男孩的腳本1: #!/bin/sh dir=/server/scripts [ ! -d $dir ] && mkdir -p $dir file="if3.sh" if [ -f "$dir/$file" ]; then echo "$file is exist." exit 0 fi touch $dir/$file 同學: [root@master4 day4]# cat tiaojian.sh #!/bin/sh dir=/server/scripts file=if3.sh cd $dir || mkdir -p $dir [ ! -f "$file" ] && touch $file || echo $file
1.4 內存告警腳本
開發腳本判斷系統內存大小,如果低於100M就郵件報警。測試報警成功後加入系統定時任務每3分鐘執行一次檢查。 常見發送郵件方法: [root@master4 day4]# mail -s "tittle" [email protected] < /etc/hosts [root@master4 day4]# echo "oldboy" | mail -s "tittle" [email protected] 安裝發郵件: yum install -y sendmail sendmail啟動慢原因 1、主機名沒有配置好 [root@master4 day4]# uname -n master4.com 10.201.106.134 master4 master4.com [root@master4 day4]# netstat -tanp | grep 25 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 15622/sendmail 解答:重視解決問題的過程 1、先再命令行把條件取出來(我的是CentOS7) [root@master4 day4]# free -m | grep Mem | awk ‘{print $NF}‘ 122 2、編寫腳本 [root@master4 day4]# vim if04.sh #!/bin/sh current_mem=`free -m | grep Mem | awk ‘{print $NF}‘` mem_chars="our sys mem is:$current_mem" if [ $current_mem -lt 200 ];then echo "$mem_chars" | mail -s "tittle" [email protected] fi [root@master4 day4]# sh -x if04.sh ++ awk ‘{print $NF}‘ ++ grep Mem ++ free -m + current_mem=178 + mem_chars=‘our sys mem is:178‘ + ‘[‘ 178 -lt 200 ‘]‘ + mail -s tittle [email protected] + echo ‘our sys mem is:178‘ 加入到定時任務: [root@master4 day4]# crontab -l #monitor memory */3 * * * * /bin/sh /server/scripts/day4/if04.sh > /dev/null 2>&1
2、雙分支
2.1 比較兩個整數大小(可用條件小寫或者大於等於)
我的腳本:
[root@master4 day4]# cat if05.sh
#!/bin/sh
if [ $1 -le $2 ];then
if [ $1 -lt $2 ];then
echo "$1 is less than $2"
else
echo "$1 is eq $2"
fi
else
echo "$1 is big than $2"
fi
[root@master4 day4]# cat if-double01.sh
#!/bin/sh
if [ $1 -lt $2 ];then
echo "$1 < $2"
else
echo "$1 >= $2"
fi
[root@master4 day4]# sh if-double01.sh 4 5
4 < 5
[root@master4 day4]# sh if-double01.sh 2 2
2 >= 2
[root@master4 day4]# sh if-double01.sh 2 1
2 >= 1
3、多分支
3.1 還是比較兩個整數大小
[root@master4 day4]# cat if-double02.sh
#!/bin/sh
read -p "please input first number:" num1
read -p "please input second number:" num2
if [ $num1 -lt $num2 ];then
echo "$num1 < $num2"
elif [ $num1 -gt $num2 ];then
echo "$num1 > $num2"
else
echo "$num1 = $num2"
fi
[root@master4 day4]# sh if-double02.sh
please input first number:4
please input second number:3
4 > 3
[root@master4 day4]# sh if-double02.sh
please input first number:4
please input second number:4
4 = 4
[root@master4 day4]# sh if-double02.sh
please input first number:4
please input second number:5
4 < 5
老男孩腳本1:
[root@master4 day4]# cat if-multi01.sh
#!/bin/sh
if [ $1 -lt $2 ];then
echo "$1 < $2"
elif [ $1 -eq $2 ];then
echo "$1 = $2"
else
echo "$1 > $2"
fi
解決命令行傳參,不加參數,報錯問題:
[root@master4 day4]# cat judge-if-multi01-argv.sh
#!/bin/sh
if [ $# -ne 2 ];then
echo "$0 Usage method: num1 num2"
exit 1
fi
if [ $1 -lt $2 ];then
echo "$1 < $2"
elif [ $1 -eq $2 ];then
echo "$1 = $2"
else
echo "$1 > $2"
fi
解決輸入非整數的問題:
過濾出數字:
[root@master4 day4]# echo "3434dkjfkddfd99" | sed ‘s#[a-z]##g‘
343499
反過來,過濾數字,看字段是否為0。把數字去掉,長度為0,說明是數字
[root@master4 day4]# echo "3434dkjfkddfd99" | sed ‘s#[0-9]##g‘
dkjfkddfd
[root@master4 day4]# cat judge-if-multi01-argv.sh
#!/bin/sh
if [ $# -ne 2 ];then
echo "$0 Usage method: num1 num2"
exit 1
fi
[ -n "`echo $1|sed ‘s#[0-9]##g‘`" ] && {
echo "num1 must be int."
exit
}
[ -n "`echo $2|sed ‘s#[0-9]##g‘`" ] && {
echo "num2 must be int."
exit
}
if [ $1 -lt $2 ];then
echo "$1 < $2"
elif [ $1 -eq $2 ];then
echo "$1 = $2"
else
echo "$1 > $2"
fi
測試:
[root@master4 day4]# sh judge-if-multi01-argv.sh nu3 34v 343
judge-if-multi01-argv.sh Usage method: num1 num2
[root@master4 day4]# sh judge-if-multi01-argv.sh nu3 34v
num1 must be int.
[root@master4 day4]# sh judge-if-multi01-argv.sh 43 34v
num2 must be int.
[root@master4 day4]# sh judge-if-multi01-argv.sh 43 3
43 > 3
[root@master4 day4]# sh judge-if-multi01-argv.sh 43 43
43 = 43
[root@master4 day4]# sh judge-if-multi01-argv.sh 43 53
43 < 53
4、判斷字符串是否為數字的多種方法
4.1 sed加正則表達式
[ -n "`echo $num|sed ‘s/[0-9]//g‘`" ] && echo "第二個參數必須為數字" && exit 1
條件表達式,大括號的用法:
[ -n "`echo $num|sed ‘s/[0-9]//‘`" ] && {
echo "第二個參數必須為數字"
exit 1
}
4.2 變量的子串替換加正則表達式
[root@master4 day4]# num=521old521
[root@master4 day4]# [ -z "`echo "${num//[0-9]/}"`" ] && echo 1 || echo 0
0
[root@master4 day4]#
這個結果說明前面的結果不為1,字符串非空,即有非數字字符
————————————————
[root@master4 day4]# num=521
[root@master4 day4]# [ -z "`echo "${num//[0-9]/}"`" ] && echo 1 || echo 0
1
這個結果說明前面的結果去掉數字後為1,即沒有非數字字符
4.3 變量的子串替換加正則表達式(特殊判斷思路)
思路:如果num長度不為0,並且把num中的非數字部分刪除,然後看結果是不是等於num本身,如果兩者都成立就是數字。
[root@master4 day4]# num=123
[root@master4 day4]# [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] && echo "it is num"
it is num
[root@master4 day4]# num=1d3
[root@master4 day4]# [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] && echo "it is num" || echo "is not is num"
is not is num
[root@master4 day4]#
4.4 expr計算判斷
把變量和整數相加看是否成功執行:
[root@master4 day4]# expr $1 + 0 > /dev/null 2>&1
[root@master4 day4]# [ $? -eq 0 ] && echo int
5、read讀入方式判斷整數大小
我的腳本:
#!/bin/sh
read -p "please input first num:" num1
read -p "please input second num:" num2
[ -n "`echo $num1|sed ‘s#[0-9]##g‘`" ] && {
echo "num1 must be int."
exit
}
[ -n "`echo $num2|sed ‘s#[0-9]##g‘`" ] && {
echo "num2 must be int."
exit
}
if [ $num1 -lt $num2 ];then
echo "$num1 < $num2"
elif [ $$num1 -eq $$num2 ];then
echo "$num1 = $num2"
else
echo "$num1 > $num2"
fi
老男孩的腳本1:
[root@master4 day4]# vim read03.sh
#!/bin/sh
read -p "please input tow num:" a b
expr $a + 0 > /dev/null 2>&1
RETVAL1=$?
expr $b + 0 > /dev/null 2>&1
RETVAL2=$?
[ "$RETVAL1" -eq 0 -a $RETVAL2 -eq 0 ] || {
echo "argv1 and argv2 must be int."
exit 2
}
if (( $a<$b ));then
echo "$a<$b"
elif [ $a -eq $b ];then
echo "$a=$b"
else
echo "$a>$b"
fi
二、MySQL監控
1、方法一
1.1 監聽端口
我的腳本1:
[root@master4 MySQL]# cat 01.sh
#!/bin/bash
# pang duan mysqld starting
if [ "`netstat -tan | grep 3306 | awk ‘{print $6}‘ | head -1`" == "LISTEN" ]; then
echo "mysqld is starting"
else
echo "mysqld is stop.Now is starting mysqld service..."
systemctl start mysqld.service
pid=$(lsof -i:3306 | head -2 | grep mysqld | awk ‘{print $2}‘)
echo "staring mysqld ok,the mysqld PID is $pid"
fi
我的腳本2:
[root@master4 MySQL]# cat 02.sh
#!/bin/bash
# pang duan mysqld starting
if [ "`systemctl status mysqld.service | grep Active | awk ‘{print $2}‘`" == "active" ]; then
echo "mysqld is starting"
else
echo "mysqld is stop.Now is starting mysqld service..."
systemctl start mysqld.service
pid=$(lsof -i:3306 | head -2 | grep mysqld | awk ‘{print $2}‘)
echo "staring mysqld ok,the mysqld PID is $pid"
fi
[root@master4 MySQL]#
老男孩的腳本1(這個方法有問題,進程被殺死,取得值為空,會報錯):
[root@master4 MySQL]# cat judgedb_by_port01.sh
#!/bin/sh
port=`netstat -lnt | grep 3306 | awk -F ‘[ :]+‘ ‘{print $5}‘`
if [ $port -ne 3306 ];then
echo "mysql is stoped"
systemctl start mysqld
fi
老男孩的腳本1【改進版】(取netstat顯示的行數):
[root@master4 MySQL]# cat judgedb_by_port01.sh
#!/bin/sh
portnum=`netstat -lnt | grep 3306 | wc -l`
if [ $portnum -ne 1 ];then
echo "mysql is stoped"
systemctl start mysqld
else
echo "mysql is running"
fi
測試:
[root@master4 MySQL]# sh judgedb_by_port01.sh
mysql is stoped
[root@master4 MySQL]# sh judgedb_by_port01.sh
mysql is running
[root@master4 MySQL]#
2、方法二
2.0 監控端口和進程
我的腳本1:
[root@master4 MySQL]# cat judgedb_by_port01.sh
#!/bin/sh
portnum=`netstat -lnt | grep 3306 | wc -l`
pid=`ps -ed | grep mysqld | wc -l`
if [ $portnum -ne 1 -o $pid -le 1 ];then
echo "mysql is stoped"
systemctl start mysqld
else
echo "mysql is running"
fi
[root@master4 MySQL]# bash -x judgedb_by_port01.sh
++ wc -l
++ netstat -lnt
++ grep 3306
+ portnum=0
++ grep mysqld
++ wc -l
++ ps -ed
+ pid=0
+ ‘[‘ 0 -ne 1 -a 0 -le 1 ‘]‘
+ echo ‘mysql is stoped‘
mysql is stoped
+ systemctl start mysqld
老男孩的腳本1:
[root@master4 MySQL]# cat judgedb_by_port02.sh
#!/bin/sh
portnum=`netstat -lnt | grep 3306 | wc -l`
processsum=`ps -ef | grep mysql | grep -v grep | wc -l`
if [[ $portnum -eq 1 || $processsum -eq 2 ]];then
echo "mysql is running"
else
echo "mysql is stoped"
echo "Staring MySQL..."
systemctl start mysqld
fi
測試:
[root@master4 MySQL]# sh -x judgedb_by_port02.sh
++ wc -l
++ netstat -lnt
++ grep 3306
+ portnum=0
++ grep -v grep
++ wc -l
++ ps -ef
++ grep mysql
+ processsum=0
+ [[ 0 -eq 1 ]]
+ [[ 0 -eq 2 ]]
+ echo ‘mysql is stoped‘
mysql is stoped
+ systemctl start mysqld
[root@master4 MySQL]#
[root@master4 MySQL]#
[root@master4 MySQL]# sh -x judgedb_by_port02.sh
++ wc -l
++ netstat -lnt
++ grep 3306
+ portnum=1
++ grep mysql
++ grep -v grep
++ ps -ef
++ wc -l
+ processsum=2
+ [[ 1 -eq 1 ]]
+ echo ‘mysql is running‘
mysql is running
[root@master4 MySQL]#
老男孩腳本2:
[root@master4 MySQL]# cat judgedb_by_port03.sh
#!/bin/bash
#function:check mysql status
MySQLSTARTUP="systemctl start mysqld.service"
DbProcessCount=`ps -ef | grep mysql | grep -v brep | wc -l`
DbPortCount=`netstat -lnt | grep 3306 | wc -l`
if [ $DbProcessCount -eq 2 ] && [ $DbPortCount -eq 1 ];then
echo "MySQL is running"
else
$MySQLSTARTUP > /tmp/mysql.log
sleep 10;
DbProcessCount=`ps -ef | grep mysql | grep -v grep | wc -l`
DbPortCount=`netstat -lnt | grep 3306 | wc -l`
if [ $DbProcessCount -ne 2 ] || [ $DbPortCount -ne 1 ];then
pkill mysqld > /dev/null 2>&1
sleep 5
pkill mysqld > /dev/null 2>&1
sleep 5
[ $DbProcessCount -eq 0 ] && MySQLSTARTUP >> /tmp/mysql.log
[ $? -eq 0 ] && echo "mysql is started"
fi
mail -s "mysql restarted" [email protected] < /tmp/mysql.log
fi
2.2 轉化腳本格式為UNIX模式
[root@master4 MySQL]# dos2unix judgedb_by_port03.sh
dos2unix: converting file judgedb_by_port03.sh to Unix format ...
3、方法三
3.0 腳本測試連接
我的腳本1:
[root@master4 MySQL]# cat 03.sh
#!/bin/sh
value=`mysql -uroot -p‘root‘ -S /var/lib/mysql/mysql.sock -e "SELECT VERSION();" > /dev/null 2>&1;echo $?`
portnum=`netstat -lnt | grep 3306 | wc -l`
processsum=`ps -ef | grep mysql | grep -v grep | wc -l`
if [[ $portnum -eq 1 && $processsum -eq 2 && value -eq 0 ]];then
echo "mysql is running"
else
echo "mysql is stoped"
echo "Staring MySQL..."
systemctl start mysqld
Version=`mysql -uroot -p‘root‘ -S /var/lib/mysql/mysql.sock -e "SELECT VERSION();" | grep -v "()"`
echo "The MySQL version is $Version"
fi
老男孩的腳本1:
[root@master4 MySQL]# cp judgedb_by_port02.sh judgedb_by_mysql04.sh
[root@master4 MySQL]# cat judgedb_by_mysql04.sh
#!/bin/sh
mysql -uroot -proot -S /var/lib/mysql/mysql.sock -e "SELECT VERSION();" >/dev/null 2>&1
if [[ $? -eq 0 ]];then
echo "mysql is running"
else
echo "mysql is stoped"
echo "Staring MySQL..."
systemctl start mysqld
fi
異地連接,使用-h IP
4、方法四
4.1 通過php/java程序監控MySQL
三、監控nginx或apache
1、
1.1 查看nginx是否運行
[root@master4 ~]# netstat -tan | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp6 0 0 :::80 :::* LISTEN
[root@master4 ~]# lsof -i :80
[root@master4 ~]# ps -ef | grep nginx
[root@node3 ~]# curl -I 10.201.106.134
[root@node3 ~]# wget 10.201.106.134
[root@node3 ~]# curl -o /dev/null -s -w "%{http_code}" 10.201.106.134
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3700 100 3700 0 0 695k 0 --:--:-- --:--:-- --:--:-- 0
curl -s 靜默顯示
[root@node3 ~]# nmap 10.201.106.134 -p 80 | grep open | wc -l
1
[root@node3 ~]# curl -o /dev/null -s -w %{http_code} http://10.201.106.134
200[root@node3 ~]#
[root@node3 ~]# curl -s -I http://10.201.106.134 | sed -n ‘1p‘ | cut -d " " -f2
200
1.2 namp判斷nginx是否啟動
[root@master4 nginx]# vim judgeweb_by_port01.sh
#!/bin/sh
portnum=`nmap 10.201.106.134 -p 80 | grep open | wc -l`
if [ $portnum -ne 1 ];then
echo "nginx is stoped"
systemctl start nginx
else
echo "nginx is running"
fi
1.3 wget
[root@master4 nginx]# vim judgeweb_by_wget-url02.sh
#!/bin/sh
wget -T 15 -q --spider http://10.201.106.134
if [ $? -ne 0 ];then
echo "nginx is stoped"
systemctl start nginx
else
echo "nginx is running"
fi
1.4 獲取狀態碼判斷
[root@master4 nginx]# vim judgeweb_by_curl-code03.sh
#!/bin/sh
httpcode=`curl -o /dev/null -s -w %{http_code} http://10.201.106.134`
if [ "$httpcode" != "200" ];then
echo "nginx is stoped"
systemctl start nginx
else
echo "nginx is running"
fi
[root@master4 nginx]# pkill nginx
[root@master4 nginx]# sh judgeweb_by_curl-code03.sh
nginx is stoped
[root@master4 nginx]# sh judgeweb_by_curl-code03.sh
nginx is running
[root@master4 nginx]#
shell學習筆記(3)