当前位置: 首页 > article >正文

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 


http://www.kler.cn/a/441921.html

相关文章:

  • 什么是 PHP 键值对
  • Java 多态的理解
  • 按类别调整目标检测标注框的写入顺序以优化人工审核效率
  • VMProtect:软件保护与安全的全面解决方案
  • LabVIEW农机自主导航监控系统
  • 【Redis篇】Set和Zset 有序集合基本使用
  • 【系统】Mac crontab 无法退出编辑模式问题
  • 企业内训|阅读行业产品运营实战训练营-某运营商数字娱乐公司
  • 游戏AI实现-寻路算法(GBFS)
  • ubuntu系统版本安装docker容器
  • Electron和C/C++开发桌面应用对比
  • 数据结构实验题目剖析·下篇(PTA平台原题)
  • springboot443旅游管理系统(论文+源码)_kaic
  • 点焊机器人维修-ABB-KUKA-FANUC-YASKAWA
  • 第四篇:HTTP 的铠甲——HTTPS 的故事
  • 家中常用的路由器密码如何更改详细教程
  • flask flask-socketio创建一个网页聊天应用
  • 编辑器kindeditor
  • 优化Lua-cURL:减少网络请求延迟的实用方法
  • git从入门到实践