当前位置:网站首页>Shell第二天作业
Shell第二天作业
2022-07-26 08:12:00 【三俩两】
1、ping主机测试,查看主机是否存活;
mkdir -p /shells/test
cd /shells/test/
vim 1.sh
#!/bin/bash
if ping -c2 192.168.227.130 &>/dev/null
then
echo "192.168.227.130 is reachable"
else
echo "192.168.227.130 is not reachable"
fi
bash 1.sh
2、判断一个用户是否存在;
vim 2.sh
#!/bin/bash
read -p "input your user:" user
id $name &>/dev/null
if [ $? -eq 0 ]
then
echo "$user exitence"
else
echo "$user exitence"
fi
bash 2.sh
3、判断当前内核主版本是否为3,且次版本是否大于10;
vim 3.sh
#!/bin/bash
main = uname -r | cut -d '.' -f 1
inmain = uname -r | cut -d '.' -f 2
if [ "$main" -eq 3 ] && [ "$inmain" -ge 10 ]
then
echo "main version is "$main" inmain version is "$inmain""
else
echo "main version is not 3 inmain version is more than 10"
fi
~
bash 3.sh
4、判断vsftpd软件包是否安装,如果没有则自动安装;
vim 4.sh
#!/bin/bash
rpm -qa | grep vsftpd >&/dev/null
if [ $? -eq 0 ]
then
echo" Installed "
else
yum isntall -y vsftpd
fi
bash 4.sh
5、判断httpd是否运行;
vim 5.sh
#!/bin/bash
status="`ps -ef | grep httpd | wc -l`"
if [ $status -ge 1 ]
then
echo "httpd is running"
else
echo "httpd is not running"
fi
bash 5.sh
6、判断指定的主机是否能ping通,必须使用$1变量;
vim 6.sh
#!/bin/bash
ping -c2 192.168.227.130 &>/dev/null
if [ "$?" -eq 0 ]
then
echo "192.168.227.130 is reachable"
else
echo "192.168.227.130 is not reachable"
fi
bash 6.sh
7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[[email protected] ~]# echo “邮件正文” | mail -s “邮件主题” alice
vim 7.sh
#!/bin/bash
root_total=`free -m | tr -s " " | cut -d" " -f2 | head -2 | tail -1`
root_used=`free -m | tr -s " " | cut -d" " -f3 | head -2 | tail -1`
mem_used=`df -hP | tr -s " " | cut -d" " -f5 | head -2 | tail -1 | cut -d"%" -f1`
root=$[$root_used / $root_total]
if [ $root -gt 80 ]
then
echo "gen less than20%" | mail -s "报警信息" alice
elif [ $mem_used -gt 80 ]
then
echo "member is more than 80%" | mail -s "报警信息" alice
else
echo "is usuall"
fi
bash 7.sh
8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10
vim 9.sh
#!/bin/bash
read -p "please input number:" num
expr $num + 1 >&/dev/null
if [ $? -eq 0 ]
then
echo "this is a number"
else
echo "this is not a number"
fi
bash 9.sh
9、计算用户输入的任意两个整数的和、差、乘积、商、余数,
判断用户输入的参数是否是两个,如果不是,提示用法;
判断用户输入的是否是整数,如果不是,则给出提示终止运行。
a=$1
b=$2
if [ $# -eq 2 ]
then
if [[ "$a" =~ ^[0-9]*$ && "$b" =~ ^[0-9]*$ ]]
then
echo "a、b is a number"
echo a+b=$((a+b))
echo a-b=$((a-b))
echo a*b=$((a*b))
echo a/b=$((a/b))
echo a%b=$((a%b))
else
echo "a、b is not a number"
exit 0
fi
else
echo "this is not two number"
exit 0
fi
边栏推荐
- Using ordered dictionary to copy pcap files
- OSPF总结
- One click deployment lamp and LNMP architecture
- 2022-07-08 group 5 Gu Xiangquan's learning notes day01
- 为啥谷歌的内部工具不适合你?
- Strtus2历史漏洞复现
- The bigger the project is, the bigger it is. This is how I split it
- 要不你给我说说什么是长轮询吧?
- Team members participate in 2022 China multimedia conference
- Why is Google's internal tools not suitable for you?
猜你喜欢

Web side 3D visualization engine hoops communicator reads 10g super large model test | digital twin Technology

R language foundation

Burp suite Chapter 4 advanced options for SSL and proxy

Recurrence of strtus2 historical vulnerability

shardingjdbc踩坑记录

Traversal mode of list, set, map, queue, deque, stack

OSPF总结

吉他五线谱联系 茉莉花

苹果强硬新规:用第三方支付也要抽成,开发者亏大了!

一键部署LAMP和LNMP架构
随机推荐
BGP的基本配置
Common database commands (special for review)
有点牛逼,一个月13万+
Official Oracle document
QT listview add controls and pictures
FTP service
CentOS install mysql5.7
Using producer consumer model and dpkt to process pcap files
The bigger the project is, the bigger it is. This is how I split it
JSP built-in object (implicit object)
我,35岁了。
Shardingjdbc pit record
2022 7/5 exam summary
OSPF总结
BGP选路原则
Summary of traversal methods of list, set, map, queue, deque and stack
Using ordered dictionary to copy pcap files
基础乐理 节奏联系题,很重要
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
Burp suite Chapter 4 advanced options for SSL and proxy