跳转至

3. if语法

3.1 单分支if

if [  -f "1.jpg"  ];then echo "yes,it is a file ";fi

条件判断

[ -f /etc/hosts ] && echo yes
[[  -f /etc/hosts  ]] && echo yes
test -f /etc/hosts && echo yes

条件判断改造为if

vim if_test.sh
#!/bin/bash
if [ -f /etc/hosts ];then
    echo "[] is ok"
fi

if [[  -f /etc/hosts  ]];then
    echo "[[]] is ok"
fi

if test -f /etc/hosts;then
    echo "test is ok"
fi

3.2 剩余内存检测脚本

vim freemem.sh
#!/bin/bash

#1.获取available内存
FreeMem=`free -m |awk 'NR==2 {print $NF}'`
CHARS="Current memory is $FreeMem"

#2.判断剩余内存
if [  "$FreeMem" -lt "2100"  ];then
    #将剩余内存写入文件,发送邮件用。
    echo $CHARS | tee /tmp/messages.txt
    echo "freemem is low..."
fi
#加入定时任务
crontab -e
*/5 * * * * /bin/bash   /myscripts/freemem.sh  &>/dev/null

3.3 俩数比较脚本

vim compare_2_num.sh
#!/bin/bash
a=$1
b=$2
if [   "$a" -lt "$b"  ];then
    echo "$a is less than $b"
fi
if [ "$a" -eq "$b" ];then
    echo "$a is equal $b"
fi
if [ "$a" -gt "$b" ];then
    echo "$a is great than $b"
fi

改造为多分支语句

#!/bin/bash
a=$1;b=$2
if [  "$a" -lt "$b"  ];then
    echo "$a is less than $b"
elif [  "$a" -eq "$b"  ];then
    echo "$a is equal $b"
else
    echo "$a is great than $b"
fi

3.4 mysql监控脚本

1.本地端口监控

netstat -tunlp |grep mysql |wc -l
ss -tunlp |grep mysql |wc -l
lsof -i tcp:3306 |wc -l

2.远程端口监控

yum install telnet nmap nc -y

nmap 127.0.0.1 -p 3306 |grep open |wc -l
echo -e "\n" |telnet 127.0.0.1 3306 2>/dev/null|grep Connected |wc -l

3.服务进程监控

ps -ef |grep mysql |grep -v grep |wc -l

4.通过编程语言连接MySQL监控

php连接MySQL

yum install php -mysqlnd php -y
vim mysql_connect.php
<?php
$mysql_id=mysql_connect("localhost","root","123456") or mysql_error();
if ($mysql_id){
    echo "connect success..."
}else{
    echo mysql_error();
}
php mysql_connect.php

python连接mysql

yum install python3 phthon3-devel phthon3-pip -y
pip3 install pymysql
vim python_mysql_connect.py
import pymysql
db=pymysql.connectP(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    db="mysql",
    charset="utf8"
)
cursor=db.cursor()
cursor.execute("select version()")
data=cursor.fetchone()

print("mysql connect successfully,the version is %s"%data)

db.close()
python3 python_mysql_connect.py

5.登录MySQL数据库执行语句判断

mysql -uroot -p123456 -e"select version();" &>/dev/null
echo $?

脚本开发

vim mysql_test.sh
#!/bin/bash
echo "-----------------method 1-----------------"
if [  `netstat -tunlp|grep mysql|wc -l`  = "1" ];then
    echo "mysql is running..."
else
    echo "mysql is stopped,now trying to restart mysqld..."
    systemctl start mariadb
fi

echo "-----------------method 2----------------------"
if [  `ss -tunlp |grep mysql | wc -l` -eq "1"  ];then
...


echo "-----------------method 3----------------------"  
if [  `lsof -i tcp:3306 |wc -l` -gt "0" ];then
...

echo "-----------------method 4----------------------"
php /myscripts/mysql_connect.php
if [  "$?" -eq 0  ];then
...

echo "-----------------method 5----------------------"
python /myscripts/python_mysql_connect.py
if [  "$?" -eq 0  ];then
...

3.5 rsync启停脚本

vim /etc/init.d/cp_rsync

#!/bin/bash
#author:chu peng
#输入错误的提示信息
if [  "$#" -ne 1  ];then
    echo "Usage: $0 {start|stop|restart}"
    exit 1
fi

#start
if [ "$1" = "start" ];then
    /usr/bin/rsync --daemon
    sleep 2
    #检测端口
    if [  `netstat -tunlp |grep rsync |wc -l` -ge 1  ];then
        echo "rsync is started "
        exit 0
    fi
elif [ "$1" = "stop" ];then
    killall rsync &>/dev/null
    sleep 2
    if [  `netstat -tunlp |grep rsync |wc -l ` -eq 0  ];then
        echo "rsync is stopped"
        exit 0
    fi
elif [  "$1" = "restart"  ];then
    killall rsync 
    sleep 1
    killpro=`netstat -tunlp |grep rsync|wc -l `
    /usr/bin/rsync --daemon
    sleep 1
    startpro=`netstat -tunlp |grep rsync |wc -l`
    if [  "$killpro"  -eq 0 -a "startpro" -ge 1 ];then
        echo "rsyncd is restarted"
        exit 0
    fi
else
    echo "Usage: $0 {start|stop|restart}"
    exit 1
fi
chmod +x /etc/init.d/cp_rsync

最后更新: 2022-02-19 13:59:07