题目要求

写一个 脚本可以接受选项[i,I],完成下面任务:

1)使用格式: [-i interface | -I ip]

2)当用户使用-i选项时,显示指定网卡的IP地址;当用户使用-I选项时,显示其指定ip所属的网卡。

例: sh -i eth0 或者

sh -I 192.168.0.1

3)当用户使用除[-i | -I]选项时,显示[-i interface | -I ip]此信息。

4)当用户指定信息不符合时,显示错误。(比如指定的eth0没有,而是eth1时)

有点小复杂,需要在多看看视频,现在还没有完全搞明白

参考答案

#!/bin/bash
#将网卡名记录到文件ifs.txt中
ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/ifs.txt
#一个函数 输入网卡名字获取ip
get_ip()
{
    ip add show dev $1 |grep inet |awk '{print $2}' |awk -F '/' '{print $1}'
}
#遍历网卡获取ip
for eth in `cat /tmp/ifs.txt`
do
    myip=`get_ip $eth`
    if [ -z "$myip" ]
    then
	echo $eth 
    else
	echo $eth $myip 
    fi
#将ip信息记录到if_ip.txt文件中
done > /tmp/if_ip.txt 

#参数少于两个的时候直接报错,告知用户使用方法
if [ $# -ne 2 ]
then
    echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"
    exit
fi

#第一个参数为-i时,获取响应网卡的ip
if [ $1 == "-i" ]
then
    #打印出所有网卡,过滤是否含有参数2的网卡
    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
#第一个参数是-I时,获取响应ip的网卡信息
elif [ $1 == "-I" ]
then
    #过滤是否含有参数输入的ip
    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

```#### 题目要求
写一个脚本产生随机3位的数字,并且可以根据用户的输入参数来判断输出几组。 比如,脚本名字为 。

执行方法:

1)bash    会产生一组3位数字。

2)bash  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],这里的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

#### 题目要求
用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

#### 题目要求
写一个监控网卡的脚本,需要满足以下要求:

1. 每10分钟检测一次指定网卡的流量。

2. 如果流量为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