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))"

在这里插入图片描述

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...