Linux——Shell

if 语句

格式:if list; then list; [ elif list; then list; ] ... [ else list; ] fi

单分支

if 条件表达式; then

        命令

fi 

示例:

#!/bin/bash

N=10 if [ $N -gt 5 ]; then

        echo yes

fi

# bash test.sh

yes 

双分支

if 条件表达式; then

        命令

else

        命令

fi 

示例

#!/bin/bash

N=10

if [ $N -lt 5 ]; then

        echo yes

else

        echo no

fi

# bash test.sh

no 

多分支

if

条件表达式; then

        命令

elif条件表达式; then

        命令

else

        命令

fi 

示例

#!/bin/bash

N=$1

if [ $N -eq 3 ]; then

        echo "eq 3"

elif [ $N -eq 5 ]; then

        echo "eq 5"

elif [ $N -eq 8 ]; then

        echo "eq 8"

else

        echo "no"

fi 

实验

 1、判断当前磁盘剩余空间是否有20G,如果小于20G,则将报警邮件发送给管理员,每天检查一次磁盘剩余空间。

1)测试是否连接外网

[root@localhost scripts]# ping baidu.com

2)保证防火墙和selinux已关闭

[root@localhost scripts]# systemctl stop firewalld.service 
[root@localhost scripts]# setenforce 0

3)安装sendmail和mailx

[root@localhost ~]# yum -y install sendmail mailx

4)启动

[root@localhost ~]# systemctl start sendmail

5)获取邮箱授权码

前往邮箱官网(POP3/SMTP/IMAP)

 找到IMAP/SMTP,开启服务(需要验证码等)。收到授权码后,复制授权码

6)编辑/etc/mail.rc文件

[root@localhost scripts]# vim /etc/mail.rc 

set from=15009281620@163.com  #对方收到邮件时显示的发件人
set smtp=smtp.163.com #163邮箱地址
set smtp-auth-user=15009281620@163.com   #邮箱账号
set smtp-auth-password=JCocvdssJMCO #授权密码
set smtp-auth=login  #smtp的认证方式,默认是login,也可以改为CRAM—MD5或PLAIN方式
 

7)测试是否能发送消息

echo 内容|mail -s“主题”收件人

[root@localhost scripts]# echo "hello " | mail -s "hello" 15009281620@163.com

8)创建脚本文件

[root@localhost scripts]# vim Mem.sh   #创建脚本文件

9)编辑脚本文件

脚本解析:

free -g | grep "Mem" | tr -s " " | cut -d" " -f 4

查看内存容量(-g)以G为单位查看,过滤“Mem”内存信息,去除空格,以空格为间隔,切除第四列信息。这就是我们需要的内存剩余容量

#!/bin/bash
##############################################################
# File Name: free.sh
# Version: V1.0
# Author: Gao_XY
# Email: Gao_XY@163.com
# Organization:https://blog.youkuaiyun.com/ens33?type=blog
# Created Time : 2024-12-12 22:29:59
# Description:
##############################################################
a=`free -g | grep "Mem" | tr -s " " | cut -d" " -f 4`  #定义变量,剩余容量为a变量
b=20                                                                  #定义需要比较的20为b变量
c="内存小于 $b  G "                                           #定义发送邮件的内容为c变量
if [ $a -le $b  ]                                                #if单分支,如果变量a小于变量b
then
 echo $c | mail -s "内存警报" 15009281620@163.com  #那么向邮箱发送邮件
fi

10)配置计划任务

[root@localhost etc]# crontab -e

0 0 */1 * * /bin/sh /scripts/Mem.sh    #每天整点,执行Mem.sh文件

11)测试成功


2、判断web服务是否运行(如果没有运行,则启动该服务并配置防火墙规则。)

1、查看进程的方式判断该程序是否运行

1)创建脚本文件

[root@localhost scripts]# vim web_course.sh

2)编辑脚本文件

脚本解析:

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

查看进程  过滤nginx  过滤掉grep自身的进程   统计行数

其中的grep -v grep 是干啥的呢 ?
很简单 ,为了去除包含grep的进程行,避免影响最终数据的正确性

#!/bin/bash
##############################################################
# File Name: web_course.sh
# Version: V1.0
# Author: Gao_XY
# Email: Gao_XY@163.com
# Organization:https://blog.youkuaiyun.com/ens33?type=blog
# Created Time : 2024-12-13 01:53:33
# Description:
##############################################################
course=` ps -ef | grep nginx | grep -v grep | wc -l`   #将过滤统计出来的进程行号进行定义
if [ $course -gt 0 ]                                        #如果进程的行号大于0
then
        echo "nginx is running..."                #那么该服务启动
else                                                        #否则启动该服务,并在防火墙放通该端口
        firewall-cmd --permanent --zone=public --add-port=80/tcp &>/etc/null
        systemctl start nginx
fi

3)测试

先停止web服务,执行脚本进行测试

查看web服务是否开启

root@localhost scripts]# systemctl is-active nginx.service 

2、通过查看端口的方式判断该程序是否运行

1)创建脚本文件

[root@localhost scripts]# vim web_port.sh

2)编辑脚本文件

脚本解析:

ss -lnupt | grep 80 &>/etc/null           #查看80端口号,把查询结果送入黑洞

$?                       # 返回上一条命令是否执行成功,0 为执行成功,非 0 则为执行失败

echo $?                #当执行该结果为0时,证明服务已启动

当服务没有启动的情况下,无法查询到80端口,“$?”就无法返回0。

#!/bin/bash
##############################################################
# File Name: web.sh
# Version: V1.0
# Author: Gao_XY
# Email: Gao_XY@163.com
# Organization:https://blog.youkuaiyun.com/ens33?type=blog
# Created Time : 2024-12-13 01:03:06
# Description:
##############################################################
ss -lnupt | grep 80 &>/etc/null        #查询80端口,将结果存入黑洞
port=`echo $?`     #定义测试指令为port变量,测试上一条指令是否执行成功

if [ $port == 0 ]        #如果结果为0
then
        echo "nginx is running..."         #那么执行成功
else                                        #否则开启服务,并配置防火墙放行该端口
        firewall-cmd --permanent --zone=public --add-port=80/tcp &>/etc/null
        systemctl start nginx
fi

3)测试

查看服务是否开启

[root@localhost scripts]# systemctl is-active nginx.service 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值