当前位置:网站首页>shell-第四天作业
shell-第四天作业
2022-07-25 10:28:00 【weixin_51808099】
1、ping主机测试,查看主机是否存活;
[[email protected] zuoye]# vim 1.sh
#!/bin/bash
ping -c2 192.168.40.128 &>/dev/null
if [ $? -eq 0 ];then
echo "is runing"
else
echo "not runing"
fi
~
2、判断一个用户是否存在;
[[email protected] zuoye]# vim 2.sh
#!/bin/bash
read -p "input user name:" name
id $name &>/dev/null
if [ $? -eq 0 ];then
echo "$name is exist "
else
echo "$name is not exist"
fi
~
3、判断当前内核主版本是否为3,且次版本是否大于10;
[[email protected] zuoye]# vim 3.sh
#!/bin/bash
main="`uname -r |cut -d . -f1`"
sub="`uname -r |cut -d . -f2`"
if [ $main -eq 3 ];then
if [ "$sub" -gt 10 ];then
echo "main is 3 and sub more than 10"
else
echo "main is 3 but sub less than 10"
fi
else
echo "main is not 3"
fi
4、判断vsftpd软件包是否安装,如果没有则自动安装;
[[email protected] zuoye]# vim 4.4.sh
#!/bin/bash
rpm -qa |grep vsftpd
if [ $? -eq 0 ];then
echo "The vsftpd is install"
else
yum -y install vsftpd
rpm -qa |grep vsftpd
fi
5、判断httpd是否运行;
[[email protected] zuoye]# vim 4.5.sh
#!/bin/bash
systemctl is-active httpd.service &>/dev/null
if [ $? -eq 0 ];then
echo "httpd is runing "
else
echo "httpd is not runing"
fi
6、判断指定的主机是否能ping通,必须使用$1变量;
[[email protected] zuoye]# vim 4.6.sh
#!/bin/bash
ping -c2 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "this ip is up"
else
echo "this ip is down"
fi
7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[[email protected] zuoye]# vim 4.7.1.sh
#!/bin/bash
sy=`df -h | grep -w / |awk {'print $5'}|tr -d %`
memory_user=`free -m|grep Mem|awk {'print $3'}`
memory_total=`free -m|grep Mem|awk {'print $2'}`
memory=`echo $memory_user*100/$memory_total|bc`
if [ "$user -gt 80" -o "$memory -gt 80" ];then
echo "your / use $sy%, your mem use $memory%" |mail -s "warning" root
fi
8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10;
[[email protected] zuoye]# vim 4.8.sh
#!/bin/bash
read -p "please input anumber :" num
expr 1 + $num
if [ $? -eq 0 ];then
if [ $num -gt 10 ];then
echo "Is nmuber and the number more than 10"
else
echo "Is number but the number less than 10"
fi
else
echo Is not number
fi
9、计算用户输入的任意两个整数的和、差、乘积、商、余数,
判断用户输入的参数是否是两个,如果不是,提示用法;
判断用户输入的是否是整数,如果不是,则给出提示终止运行。
#!/bin/bash
[ $# -ne 2 ] && {
echo "usage: $0 num1 num2"
exit 1
}
expr $1 + $2 + 10 &> /dev/null
if [ $? -ne 0 ]
then
echo "you must input two number"
exit 2
fi
echo "a+b=$(($1+$2))"
echo "a-b=$(($1-$2))"
echo "a*b=$(($1*$2))"
echo "a/b=$(($1/$2))"
边栏推荐
- Ue4.26 source code version black screen problem of client operation when learning Wan independent server
- B2B2C多商户系统功能丰富,极易二开!!!
- PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
- Learn NLP with Transformer (Chapter 2)
- NowCoderTOP7-11——持续更新ing
- The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
- txt转csv文件,隔行出现空行
- 100W了!
- 基于cornerstone.js的dicom医学影像查看浏览功能
- HCIP(13)
猜你喜欢

mysql事务是什么

Flask框架——消息闪现

从开源的视角,解析SAP经典ERP “三十年不用变”的架构设计

The idea has been perfectly verified again! The interest rate hike is approaching, and the trend is clear. Take advantage of this wave of market!

Learn NLP with Transformer (Chapter 6)

ESP8266 使用 DRV8833驱动板驱动N20电机

机智云物联网平台 STM32 ESP8266-01S 简单无线控灯

ArcMap无法启动解决方法

NowCoderTOP7-11——持续更新ing

Visual thematic map of American airport go style: ArcGIS Pro version
随机推荐
The integration of two in one has a long way to go
Learn NLP with Transformer (Chapter 7)
学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法
微信小程序版本更新如何通知用户?
Flame framework - Flame WTF form: file upload, verification code
30000 word express Servlet
树形动态规划
三万字速通Servlet
Reinforcement Learning 强化学习(三)
HCIP (01)
Tree dynamic programming
【flask高级】结合源码解决flask经典报错:Working outside of application context
Learn NLP with Transformer (Chapter 3)
【域泛化】2022 IJCAI领域泛化教程报告
Learn NLP with Transformer (Chapter 4)
[information system project manager] thought map series essence summary
我,AI博士生,在线众筹研究主题
JS collection
上周热点回顾(7.18-7.24)
tensorflow入门