shell流程控制之条件判断练习

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 ];then
        echo "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 ];then
        echo "$username exist"
else
        echo "$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 ];then
        if [ $var2 -gt 10 ];then
                echo "Kernel Major Version is 3 and Kernel minor version greater then 10"
        else
                echo "Kernel Major Version is 3 and kernel minor version less then 10"
        fi
else
        echo "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 ];then
        yum install -y vsftpd &>/dev/null
        echo "loading ....."
        echo "complete!"
else
        echo "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 ];then
        echo "httpd is running"
else
        echo "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  ];then
        echo "ping success!"
else
        echo "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 ]
then
        echo "Use_root_space: $Use_root_space%" | mail -s "Root_space_warning" root
fi
if [ $Use_mem -ge $Limit_mem ]
then
        echo "Use_mem:  $Use_memM" | mail -s "Use_mem_warning" root
fi
if [ $Use_root_space -lt 6 -a  $Use_mem -lt $Limit_mem ]
then
        echo "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 ];then
        echo "you must input a number"
        exit 0
fi
if [ $num1 -gt 10 ]
then
        echo "the number greater than 10"
else
        echo "then number less than 10"
fi

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

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

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值