shell流程控制之条件判断练习
创始人
2024-05-04 00:41:12
0

1、ping主机测试,查看主机是否存活;

#!/bin/bash
#########################
#File name:ping.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-28 05:06:21 
#Description: 
#########################
var=$( ifconfig ens33 | grep inet | grep -v inet6 | tr -s ' '| cut -d ' ' -f 3 )
ping -c 4 "$var" &>/dev/null
if [ $? -eq 0 ];thenecho "ping success" 
else  echo "ping failed"
fi  

运行结果
在这里插入图片描述
2、判断一个用户是否存在;

#!/bin/bash
#########################
#File name:userExist.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-28 05:33:33
#Description:
#########################
read -p "please input your username:" username
id $username &>/dev/null
if [ $? -eq 0 ];thenecho "$username exist"
elseecho "$username not exist"
fi

运行结果
在这里插入图片描述
3、判断当前内核主版本是否为3,且次版本是否大于10;

#!/bin/bash
#########################
#File name:procVersion.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-28 05:53:17
#Description:
#########################
var1=$(cat /proc/version | cut -d ' ' -f 3 | cut -d '.' -f 1)
var2=$(cat /proc/version | cut -d ' ' -f 3 | cut -d '.' -f 2)
if [ $var1 -eq 3 ];thenif [ $var2 -gt 10 ];thenecho "Kernel Major Version is 3 and Kernel minor version greater then 10"elseecho "Kernel Major Version is 3 and kernel minor version less then 10"fi
elseecho "Kernel Major Versin is not 3"
fi

运行结果
在这里插入图片描述
4、判断vsftpd软件包是否安装,如果没有则自动安装;

#!/bin/bash
#########################
#File name:package.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-28 06:44:13
#Description:
#########################
yum list installed | grep vsftpd &>/dev/null
if [ $? -ne 0 ];thenyum install -y vsftpd &>/dev/nullecho "loading ....."echo "complete!"
elseecho "downloaded"
fi

在这里插入图片描述

5、判断httpd是否运行,若没有运行自动运行;

#!/bin/bash
#########################
#File name:server.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-28 07:33:04
#Description:
#########################
var1=$( ps -ef | grep httpd | grep -v grep | wc -l )
if [ $var1 -ge 1 ];thenecho "httpd is running"
elseecho "httpd is not running"systemctl start httpd &>/dev/null
fi

在这里插入图片描述
6、判断指定的主机是否能ping通,必须使用$1变量;

#!/bin/bash
#########################
#File name:ping_var.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-30 05:17:24
#Description:
#########################
ping -c 4 $1 &>/dev/null
if [ $? -eq 0  ];thenecho "ping success!"
elseecho "ping failed!"
fi

7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户root发送告警邮件
配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” root

[root@localhost practise1]# vim /etc/crontab
#!/bin/bash
#########################
#File name:mail.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-31 00:43:52
#Description:
#########################
Use_root_space=$(df -Th | grep /$ | awk '{print $6}' | cut -d '%' -f 1)
Total_mem=$(free -m | grep Mem | awk '{print $2}')
Use_mem=$(free -m | grep Mem | awk '{print $3}')
#echo $Use_root_space $Total_mem $Use_mem
Limit_mem=$((Total_mem/100))
#echo $Limit_mem
if [ $Use_root_space -ge 6 ]
thenecho "Use_root_space: $Use_root_space%" | mail -s "Root_space_warning" root
fi
if [ $Use_mem -ge $Limit_mem ]
thenecho "Use_mem:  $Use_memM" | mail -s "Use_mem_warning" root
fi
if [ $Use_root_space -lt 6 -a  $Use_mem -lt $Limit_mem ]
thenecho "Use_root_space: $Use_space_space% Total_mem: $Total_memM Use_mem: $Use_mem"
[root@localhost practise1]# vim /etc/crontab
*/5 * * * * root     /root/test/practise1/mail.sh

在这里插入图片描述

8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10;

#!/bin/bash
#########################
#File name:number.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-12-31 21:05:33
#Description:
#########################
read -p "please input a number:" num1
echo "$num1" | grep -E ^[0-9]+$
if [ $? -ne 0 ];thenecho "you must input a number"exit 0
fi
if [ $num1 -gt 10 ]
thenecho "the number greater than 10"
elseecho "then number less than 10"
fi

9、计算用户输入的任意两个整数的和、差、乘积、商、余数,
判断用户输入的参数是否是两个,如果不是,提示用法;
判断用户输入的是否是整数,如果不是,则给出提示终止运行。

read -p "please input two integer:" num1 num2
if [ -z "$num1" -o  -z "$num2" ]
thenecho "you must input two integer!"exit 1
fi
echo "$num1$num2" | grep -E ^[0-9]+$ &>/dev/null
if [ $? -ne 0 ]
thenecho "you must input integer!"exit 1
fi
sum=$((num1+num2))
echo "a+b=$sum"  
if [ $num1 -ge $num2 ]
thenecho "a-b=$((num1-num2))"
elseecho "b-a=$((num2-num1))"
fi
echo "a*b=$((num1*num2))"
echo "a/b=$((num1/num2))"
echo "a%b=$((num1%num2))"

在这里插入图片描述

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...