首页 > 系统服务 > 详细

shell编程实战12

时间:2019-09-10 23:59:11      阅读:42      评论:0      收藏:0      [点我收藏+]

标签:add   car   lis   alt   ret   数字   

1. 获得网卡或ip

#!/bin/bash

#ip addr|grep ‘^[0-9]‘|awk -F ‘:‘ ‘{print $2}‘ >/tmp/netcard.list
#ip addr|grep -w ‘inet‘|awk -F ‘/‘ ‘{print $1}‘|awk ‘{print $2}‘> /tmp/ip.list

ip addr|awk -F ‘:‘ ‘$1 ~ "^[0-9]" {print $2}‘ >/tmp/netcard.list

get_ip()
{
ip addr show dev $1|grep ‘inet ‘|awk ‘{print $2}‘|awk -F ‘/‘ ‘{print $1}‘
}

## 获得网卡和对应ip的列表文件
for eth in `cat /tmp/netcard.list`
do
myip=`get_ip $eth`
if [ -z "$myip" ]
then
echo $eth
else
echo $eth $myip
fi
done > /tmp/ip_netcard.list

if [ $# -ne 2 ]
then
echo "Usage: sh $0 [-i interface|-I ip]"
exit
fi

if [ $1 == "-i" ]
then
if awk ‘{print $1}‘ /tmp/ip_netcard.list |grep -qw $2
then
eth=$2
ip1=`awk -v aeth=$eth ‘$1==aeth‘ /tmp/ip_netcard.list |sed "s/$eth //"`
echo "网卡$2的ip是 $ip1"
else
echo "你指定的网卡不存在,系统中的网卡有:`cat /tmp/netcard.list|xargs`"
exit
fi
elif [ $1 == "-I" ]
then
if grep -qw " $2 " /tmp/ip_netcard.list
then
eth=`grep -w " $2 " /tmp/ip_netcard.list|awk ‘{print $1}‘`
echo "IP $2对应的网卡是$eth"
else
echo "你指定的ip不对,系统中的IP有:`ip addr|grep -w ‘inet‘|awk ‘{print $2}‘|awk -F ‘/‘ ‘{print $1}‘|xargs`"
fi
else
echo "Usage: sh $0 [-i interface|-I ip]"
exit
fi

2. 产生随机3位数

#!/bin/bash

# n=$[$RANDOM%1000] ?
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 "Usage: bash $0 [n]"
fi

3. 检查和安装http 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

4. 判断日期是否合法

#!/bin/bash

if [ $# -ne 1 ] || [ $# -ne 8 ]
then
echo "Usage: 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 "你给是日期年或月是不合法的"
if

5. 检测网卡流量

 技术分享图片

 

shell编程实战12

标签:add   car   lis   alt   ret   数字   

原文:https://www.cnblogs.com/tanzhirong/p/11503884.html

(2)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号