shell训练营Day21
练习46
写一个getinterface.sh 脚本可以接受选项[i,I],完成下面任务:
1)使用格式:getinterface.sh [-i interface | -I ip]
2)当用户使用-i选项时,显示指定网卡的IP地址;当用户使用-I选项时,显示其指定ip所属的网卡。
例:
sh getinterface.sh -i eth0 或者
sh getinterface.sh -I 192.168.0.1
3)当用户使用除[-i | -I]选项时,显示[-i interface | -I ip]此信息。
4)当用户指定信息不符合时,显示错误。(比如指定的eth0没有,而是eth1时)
#!/bin/bash
ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/ifs.txt
get_ip()
{
ip add show dev $1 |grep inet |awk '{print $2}' |awk -F '/' '{print $1}'
}
for eth in
cat /tmp/ifs.txt
do
myip=
get_ip $eth
if [ -z "$myip" ]
then
echo $eth
else
echo $eth $myip
fi
done > /tmp/if_ip.txt
if [ $# -ne 2 ]
then
echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"
exit
fi
if [ $1 == "-i" ]
then
if awk '{print $1}' /tmp/if_ip.txt |grep -qw $2
then
eth=$2
ip1=
awk -v aeth=$eth '$1==aeth' /tmp/if_ip.txt|sed "s/$eth //"
echo "网卡$2的ip是 $ip1"
else
echo "你指定的网卡不对,系统中的网卡有:
cat /tmp/ifs.txt|xargs"
exit
fi
elif [ $1 == "-I" ]
then
if grep -qw " $2 " /tmp/if_ip.txt
then
eth=
grep -w " $2 " /tmp/if_ip.txt|awk '{print $1}'
echo "IP $2对应的网卡是$eth"
else
echo "你指定的ip不对,系统中的IP有:
ip add |grep inet |awk '{print $2}'|awk -F '/' '{print $1}'|xargs"
exit
fi
else
echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"
fi
练习47
写一个脚本产生随机3位的数字,并且可以根据用户的输入参数来判断输出几组。 比如,脚本名字为 number3.sh。
执行方法:
1)bash number3.sh 会产生一组3位数字。
2)bash number3.sh 10 会产生10组3位数字。
#!/bin/bash
get_number()
{
for i in
seq 0 2
do
a[$i]=$[$RANDOM%10]
done
echo ${a[@]}|sed s'/ //g'
}
if [ $# -eq 0 ]
then
get_number
elif [ $# -eq 1 ]
then
n=
echo $1|sed 's/[0-9]//g'
if [ -n "$n" ]
then
echo "给定的参数必须是一个数字"
exit
fi
for i in
seq 1 $1
do
get_number
done |xargs
else
echo "格式不对,正确的是格式是sh $0
,这里的n是一个数字。"
fi
写一个shell,先判断是否安装httpd和mysql,没有安装进行安装,安装了检查是否启动服务,若没有启动则需要启动服务。
#!/bin/bash
if_install()
{
rpm -q $1 >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo "$1已经安装"
return 0
else
echo "$1没有安装"
return 1
fi
}
if_install httpd
if [ $? -eq 0 ]
then
if ! pgrep httpd >/dev/null
then
service httpd start
fi
else
yum install -y httpd
fi
if_install mysql-server
if [ $? -eq 0 ]
then
if ! pgrep mysqld >/dev/null
then
service mysqld start
fi
else
yum install -y mysql-server
fi
练习49
用shell脚本判断输入的日期是否合法。
比如20170110就是合法日期,20171332就不合法。
#!/bin/bash
if [ $# -ne 1 ] || [ ${#1} -ne 8 ]
then
echo "请输入正确的格式,sh $0 yyyymmdd"
exit 1
fi
y=
echo ${1:0:4}
m=
echo ${1:4:2}
d=
echo ${1:6:2}
if echo $d|grep -q "^0"
then
d=
echo ${1:6:2}|sed 's/^0//'
fi
if cal $m $y >/dev/null 2>/dev/null
then
if ! cal $m $y|grep -qw "$d"
then
echo "你给的日期是不合法的"
else
echo "日期合法"
fi
else
echo "你给的日期不合法"
fi
练习50
写一个监控网卡的脚本,需要满足以下要求:
-
每10分钟检测一次指定网卡的流量。
- 如果流量为0,则重启网卡。
#!/bin/bash
LANG=en
sar -n DEV 1 10|grep -w "$1" > /tmp/sar.tmp
in=
grep "Average:" /tmp/sar.tmp|awk '{print $5}'|sed 's/\.//'
out=
grep "Average:" /tmp/sar.tmp|awk '{print $6}'|sed 's/\.//'
if [ $in == "000" ] && [ $out == "000" ]
then
ifdown $1
ifup $1
fi
- SHELL训练营--day21_shell练习46-50
- shell实战训练营Day3
- shell训练营 Day7
- shell实战训练营Day9
- shell训练营Day10
- Shell训练营Day1
- Shell实战训练营Day7
- shell实战训练营Day10
- SHELL训练营--day11_shell练习
- shell实战训练营Day19
- shell实战训练营Day1
- SHELL训练营--day3
- SHELL训练营--day6__shell脚本(2)
- Shell实战训练营Day11
- SHELL训练营--day19_shell练习36-40
- shell训练营DAY1作业
- SHELL训练营--day1
- shell训练营Day13
- shell训练营日常打卡DAY1
- shell训练营Day4