shell if学习日记

1、基本语法:

01、
if [ 条件表达式 ]
then
     符合该条件执行的语句
Fi
02、
if [ 条件表达式 ];then
     符合该条件执行的语句
fi

        if语句的“分支”指的是不同测试结果所对应的执行语句(一条或多条)。对于单分支的选择结构,只有在“条件成立”时才会执行相应的代码,否则不会执行任何操作。单分支if语句的语法格式如下所示
在这里插入图片描述

2、双分支if语句

if [ 条件表达式 ];then
     符合该条件执行的语句
elif [ 条件表达式 ];then
     符合该条件执行的语句
else
     符合该条件执行的语句
fi

        对于双分支的选择结构,要求针对“条件成立” “条件不成立”两种情况分别执行不同的操作。双分支if语句的语法格式如下所示:
在这里插入图片描述

3、多分支if语句

        由于if语句可以根据测试结果的成立、不成立分别执行操作,所有能够嵌套使用,进行多次判断。多分支if语句的语法格式如下:

在这里插入图片描述

4、实例

1、查看当前操作系统类型

#!/bin/bash
system=`uname -s`
if [ "$system"="Linux" ];then
	echo "Linux"
elif [ "$system" ="FreeBSD" ] ; then
   echo "FreeBSD"
elif [ "$system" ="Solaris" ] ; then
    echo "Solaris"
elif [ "$system" ="Windows" ] ; then
    echo "Windows"
else
    echo  "What?"
fi

2、if利用read传参判断

#!/bin/bash
read -p "please  input a score:" num

if [ $num -ge 0 ]&&[ $num -lt 60 ];then
    echo  "sorry,you  are lost!"
elif [ $num -ge 60 ]&&[ $num -lt 85 ];then
    echo "just  soso!"
elif [ $num -le 100 ]&&[ $num -ge 85 ];then
     echo "good  job!"
else
     echo "input  score is wrong , the range is [0-100]!"
fi

3、判断文件是否存在

#!/bin/bash
if [ ! -d "/etc" ]; then
        `mkdir -p /etc`
        echo "/etc文件不存在,已经自动创建"
else
        echo “该目录已经存在,不执行任何操作”
fi  

4、监控 Web 和数据库

        用 if 条件语句针对 Nginx Web 服务或 MySQL 数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务。

1)监控 Web 服务和 MySQL 数据库服务是否异常的常见方法:

    端口监控
          在服务器本地监控服务端口的常见命令有 netstat、ss、lsof
          从远端监控服务器本地端口的命令有 telnet、nmap、nc
    监控服务进程或进程数
    此方法适合本地服务器,过滤的是进程的名字。命令为:

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

    在客户端模拟用户访问
    使用 wget 或 curl 命令进行测试(如果监控数据库,则需要转为通过 Web 服务器去访问数据库),并对测试结果做三种判断:
            利用返回值(echo $?)
            获取特殊字符串(需事先开发程序)
            根据 HTTP 响应 header 的情况
    登录 MySQL 数据库判断
    通过 MySQL 客户端连接数据库,根据返回值或返回内容判断。
    例如:

mysql -uroot -pylt -e “select version();&>/dev/null; echo $?
2)监控 mysql 数据库异常

端口监控:

[root@test etc]# systemctl start mariadb
[root@test etc]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2021-04-15 18:12:58 CST; 32s ago
  Process: 1921 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1838 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 1920 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─1920 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─2085 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/us...

Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: MySQL manual for more instructions.
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: Please report any problems at http://mari...ra
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: The latest information about MariaDB is a.../.
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: You can find additional information about...t:
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: http://dev.mysql.com
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: Consider joining MariaDB's strong and vib...y:
Apr 15 18:12:56 test mariadb-prepare-db-dir[1838]: https://mariadb.org/get-involved/
Apr 15 18:12:57 test mysqld_safe[1920]: 210415 18:12:57 mysqld_safe Logging to '/var/log/ma...g'.
Apr 15 18:12:57 test mysqld_safe[1920]: 210415 18:12:57 mysqld_safe Starting mysqld daemon ...sql
Apr 15 18:12:58 test systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

[root@test etc]# netstat -lntup|grep 3306|wc -l
1
[root@test etc]# netstat -lntup|grep mysql|wc -l
1
[root@test etc]# 
[root@test etc]# ss -lntup|grep mysql|wc -l
1
[root@test etc]# ss -lntup|grep 3306|wc -l
1
[root@test etc]# lsof -i tcp:3306|wc -l
2

远端监控服务器监控本地端口:

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

服务器进程或进程数进行监控(适合本地服务器):

[root@test etc]# ps -ef|grep mysql|grep -v grep|wc -l
2
3)开发监控 MySQL 数据库的脚本
#!/bin/bash
#if [ `netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]; then
#if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]; then
#if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]; then
#if [ `lsof -i tcp:3306|wc-l` -gt 0 ]; then
if [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]; then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
4)监控 Nginx Web 服务异常

端口监控:

/application/nginx/sbin/nginx
netstat -lntup|grep nginx|wc -l
1
netstat -lntup|grep 80|wc -l
1
ss -lntup|grep -w 80|wc -l
1
lsof -i tcp:80|wc -l
3
服务器进程或进程数进行监控(适合本地服务器):
ps -ef|grep nginx|grep -v grep|wc -l
2
5)开发监控 Nginx 的脚本
#!/bin/bash
#if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ]; then
#if [ `netstat -lntup|grep -w 80|wc -l` -eq 80 ]; then
#if [ `lsof -i tcp:80|wc -l` -gt 0 ]; then
if [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ]; then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /application/nginx/sbin/nginx
fi

5、开发 rsync 服务的启动脚本

该脚本可能会存在一些小问题,请各位大佬指正!!!

#!/bin/bash
prog="rsync"
pidfile=/var/run/rsyncd.pid
exec=/usr/bin/rsync
. /etc/init.d/functions
[ -f /usr/bin/rsync ] || echo "please install and config rsync!"

if [ $# -ne 1 ]; then
        echo $"USAGE:$0 {start|stop|restart}"
        exit 1
fi
if [ "$1" = "start" ]; then
		$exec --daemon >/dev/null 2>&1
  		if [ -s "$pidfile" ]
   		then
   			action "rsync haved already been running" /bin/false
  		else
    		$exec --daemon >/dev/null 2>&1
    		action "start rsyncd:" /bin/true
  		fi
elif [ "$1" = "stop" ]; then
		if [ -f "$pidfile" ]
   			then     
   			kill -USR2 `cat $pidfile`
    		rm -f ${pidfile}
    		action "stop rsyncd:" /bin/true
  		else
    		action "rsync no running!" /bin/false
  		fi
elif [ "$1" = "restart" ]; then
       if [ -f "$pidfile" ]
   			then     
   			kill -USR2 `cat $pidfile`
    		rm -f ${pidfile}
    		action "stop rsyncd:" /bin/true
  		else
    		action "rsync no running!" /bin/false
  		fi
  		sleep 2 
        $exec --daemon >/dev/null 2>&1
  		if [ -s "$pidfile" ]
   		then
   			action "rsync haved already been running" /bin/false
  		else
    		$exec --daemon >/dev/null 2>&1
    		action "start rsyncd:" /bin/true
  		fi
else
        echo $"USAGE:$0 {start|stop|restart}"
        exit 2
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值