Shell-if判断

shell的if判断

if判断语法(单分支)

## 1.语法一:
if [ 条件 ];then
  cmd1
  cmd2
fi

## 2.语法二:
if [ 条件 ]
then
  cmd1
  cmd2
fi

if判断语法(多分支)

#1.多分支语法
if [ 条件 ];then
  cmd1
  cmd2
elif [ 条件2 ];then
  cmd1
  cmd2
elif [ 条件3 ];then
  cmd1
  cmd2
else
  cmd3
  cmd4
fi


#2.if嵌套
if [ 条件1 ];then
  cmd1
  cmd2
  if [ 条件1.1 ];then
    cmd3
    cmd4
  elif [ 条件1.2 ];then
    cmd5
    cmd6
  else
    cmd7
    cmd8
  fi
elif [ 条件2 ];then
  cmd9
  cmd10
else
  cmd13
  cmd14
  if [ 条件3.1 ];then
    cmd11
    cmd12
  fi
fi

if判断企业实战

监控系统内存,如果不足30%就发送邮件告警通知运维人员

# 邮件授权码 remfmmeddhgncafd
vim /etc/mail.rc
set from=372359276@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=372359276@qq.com
set smtp-auth-password=remfmmeddhgncafd
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

# free -m
# dd </dev/zero >/dev/null bs=500M count=1000

[root@m01 app]# free -m|awk '/Mem/{print $2}'   total
972
[root@m01 app]# free -m|awk '/Mem/{print $3}'    used
373

#发邮件命令
echo '邮件内容' | mail -s '邮件标题' 收件人
-s:邮件标题
-c:抄送
-a:加附件
mem_used=`free -m|awk '/Mem/{print $3}'`
mem_total=`free -m|awk '/Mem/{print $2}'`
awk "BEGIN{print int($mem_used/$mem_total*100)}"

## 压内存
dd </dev/zero >/dev/null bs=500M count=1000

## 脚本内容
#!/bin/bash

mem_used=`free -m|awk '/Mem/{print $3}'`
mem_total=`free -m|awk '/Mem/{print $2}'`
ip_info=`ifconfig eth0|awk 'NR==2{print $2}'`

if [ ${#mem_used} -eq 0 -o ${#mem_total} -eq 0 ];then
  awk "BEGIN{print int(${mem_used:-0}/${mem_total:-1}*100)}"
else
  mem_info=`awk "BEGIN{print int(${mem_used:-0}/${mem_total:-1}*100)}"`
  if [ $mem_info -gt 80 ];then
    echo -e "服务器内容使用率超过:80%\n当前内存使用率: ${mem_info}%" \
    |mail -s "${ip_info}:${HOSTNAME}:内存告警" 133411023@qq.com
  fi
fi

nginx服务及业务是否正常

#1.本地检测端口
netstat:netstat -lntup|grep -w '80'
ss: ss -lntup|grep -w '80'
lsof:lsof -i:80

#2.远程检测端口
echo '' |telnet 10.0.0.61 80 2>/dev/null |grep -w 'Connected'|wc -l
echo ''|nc 10.0.0.61 80
echo ''|nc 10.0.0.61 81

#3.curl
-I:只显示响应头部信息
-w:按照指定格式输出内容
-H:修改响应头部信息
-L:追中跳转内容
-X:指定请求方式
-o:将内容重定向到指定位置(下载文件)
-A:指定user-agent,伪装
-u:指定用户名和密码
curl http://zls:zls@10.0.0.61
curl -u zls:zls http://10.0.0.61
curl -s -I 10.0.0.61|awk 'NR==1{print $2}'
curl -s -I 10.0.0.61 -w %{http_code} -o /dev/null

需求分析

#1.检测进程
ps -ef

#2.检测端口
netstat

#3.检测网站是否能正常访问
curl

### 注意:检测服务,永远不要把服务名写到脚本名里
check_nginx.sh
ps -ef

#!/bin/bash
process_count=`ps -ef|grep -c [n]ginx`
port_count=`netstat -lntup|grep -wc '80'`
status_code=`curl -s -I http://zls:zls@10.0.0.61 -w %{http_code} -o /dev/null`

if [ $process_count -eq 0 ];then
  echo "进程不存在"
elif [ $port_count -eq 0 ];then
  echo "端口不存在"
elif [[ $status_code =~ ^4|^5 ]];then
  echo -e "服务挂了\n当前状态码是: $status_code"
else
  echo "业务正常"
fi

rsync服务启动脚本

#1.rsync服务部署
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
pid file = /var/run/rsyncd.pid
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup

#2.rsync启动命令
rsync --daemon

#3.rsync停止命令
kill -3 `cat /var/run/rsyncd.pid`

vim /etc/init.d/rsyncd
#!/bin/bash

# chkconfig: 2345 99 98
# File Name: __rsyncd.sh__
# Version: __v1.1__
# Author: __DriverZeng__
# Mail: __133411023@qq.com__
# Blog: __https://blog.driverzeng.com__
# DateTime: __2021-09-09 17:46__

. /etc/init.d/functions

option=$1
pid_file="/var/run/rsyncd.pid"
process_count=`ps -ef|grep -cw [r]sync`

if [ $# -ne 1 ];then
  echo "Usage: $0 {start|stop|restart}"
else
  if [ -f $pid_file -a $process_count -eq 0 ];then
    rm -f $pid_file
  fi
  if [ $option == 'start' ];then
    process_count=`ps -ef|grep -cw [r]sync`
    if [ $process_count -gt 0 ];then
      action "rsyncd running..." /bin/true
    else
      rsync --daemon &>/dev/null
      action "rsyncd start" /bin/true
    fi
  elif [ $option == 'stop' ];then
    kill -3 `cat $pid_file`
    rm -f $pid_file
    process_count=`ps -ef|grep -cw [r]sync`
    if [ $process_count -eq 0 ];then
      action "rsyncd stop" /bin/true
    else
      action "rsyncd stop" /bin/false
    fi
  elif [ $option == 'restart' ];then
    process_count=`ps -ef|grep -cw [r]sync`
    if [ $process_count -gt 0 ];then
      kill -3 `cat $pid_file`
      rm -f $pid_file
      action "rsyncd stop" /bin/true
      rsync --daemon
      action "rsyncd start" /bin/true
    else
      action "rsyncd stop" /bin/false
      rsync --daemon
      action "rsyncd start" /bin/true
    fi
  else
    echo "Usage: $0 {start|stop|restart}"
  fi
fi

if判断独家练习

这个是根据我上家公司的一个企业级需求:请监控Linux系统中重要的文件,如果文件内容被改动,权限被修改,通过发邮件的方式,告知运维人员需要尽快处理,并且要告知运维人员,是哪里被改动了,如果是内容,请告知运维人员,第几行的内容。

需求解析:

1.监控文件 /etc/passwd
2.监控内容 
3.监控权限 
4.发送邮件    
  - 时间    
  - 主机    $HOSTNAME
  - IP    
  - 文件位置    
  - 第几行    grep -n
  - 什么权限   stat -c %a /etc/passwd
5.如果是创建了用户,请告诉运维人员,什么时间创建了什么用户?uid gid 等... 

#
file_mode=`stat -c %a /etc/passwd`
file_owner=`stat -c %U /etc/passwd`
file_group=`stat -c %G /etc/passwd`
now_time=`date +%F\ %T`
ip_addr=`ifconfig eth0|awk 'NR==2{print $2}'`
add_user=`grep -cnvf /tmp/passwd /etc/passwd`
del_user=`grep -cnvf /etc/passwd /tmp/passwd`

内容

#!/bin/bash
# File Name: __check_file.sh__
# Version: __v1.1__
# Author: __DriverZeng__
# Mail: __133411023@qq.com__
# Blog: __https://blog.driverzeng.com__
# DateTime: __2021-09-10 08:58__
check_file="/etc/passwd"
origin_file="/tmp/passwd"
file_mode=`stat -c %a $check_file`
file_owner=`stat -c %U $check_file`
file_group=`stat -c %G $check_file`
now_time=`date +%F\ %T`
ip_addr=`ifconfig eth0|awk 'NR==2{print $2}'`
add_user=`grep -cnvf $origin_file $check_file`
del_user=`grep -cnvf $check_file $origin_file`
recive_user="133411023@qq.com"

if [ $file_mode -ne 644 ];then
  echo -e "${now_time}修改了 ${check_file} 文件权限\n权限被改为:${file_mode}" \
  |mail -s "${ip_addr}_${HOSTNAME}_权限被修改" $recive_user
fi

if [ $file_owner != 'root' ];then
  echo -e "${now_time}修改了 ${check_file} 属主\n属主被改为:${file_owner}" \
  |mail -s "${ip_addr}_${HOSTNAME}_属主被修改" $recive_user
fi

if [ $file_group != 'root' ];then
  echo -e "${now_time}修改了 ${check_file} 文件属组\n属组被改为:${file_group}" \
  |mail -s "${ip_addr}_${HOSTNAME}_属组被修改" $recive_user
fi

if [ $add_user -gt 0 ];then
  line=`grep -nvf $origin_file $check_file|awk -F: '{print $1}'`
  user_name=`grep -nvf $origin_file $check_file|awk -F: '{print $2}'`
  uid=`grep -nvf $origin_file $check_file|awk -F: '{print $4}'`
  gid=`grep -nvf $origin_file $check_file|awk -F: '{print $5}'`
  echo -e "${now_time}修改了 ${check_file} 创建了用户\n行号:${line}\n用户名:${user_name}\nUID:${uid}\nGID:${gid}" \
  |mail -s "${ip_addr}_${HOSTNAME}_创建了用户" $recive_user
  cat $check_file > $origin_file
elif [ $del_user -gt 0 ];then
  line=`grep -nvf $check_file $origin_file |awk -F: '{print $1}'`
  user_name=`grep -nvf $check_file $origin_file|awk -F: '{print $2}'`
  uid=`grep -nvf $check_file $origin_file|awk -F: '{print $4}'`
  gid=`grep -nvf $check_file $origin_file|awk -F: '{print $5}'`
  echo -e "${now_time}修改了 ${check_file} 删除了用户\n行号:${line}\n用户名:${user_name}\nUID:${uid}\nGID:${gid}" \
  |mail -s "${ip_addr}_${HOSTNAME}_删除了用户" $recive_user
  cat $check_file > $origin_file
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值