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.csdn.net/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.csdn.net/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.csdn.net/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

相关推荐
A小辣椒16 小时前
TShark:Wireshark CLI 功能
linux
A小辣椒20 小时前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式