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

在这里插入图片描述

原文地址:https://blog.csdn.net/m0_47218990/article/details/128500601

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20954.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注