shell编程实战12
其他
2019-09-11 00:21:11
阅读次数: 0
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. 检测网卡流量
转载自www.cnblogs.com/tanzhirong/p/11503884.html