BASH第七课第一题
2015-08-24 09:27
579 查看
1、写一个脚本getinterface.sh,脚本可以接受参数(i,I,a),完成以下任务:
(1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]
(2)当用户使用-i选项时,显示其指定网卡的IP地址;
(3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;(如 192.168.199.183:eth0)
(4)当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)
(1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]
(2)当用户使用-i选项时,显示其指定网卡的IP地址;
(3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;(如 192.168.199.183:eth0)
(4)当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)
#!/bin/bash exec 2>>/dev/null command=$1 list=`netstat -i | sed -n '3,65535p'|awk -F" " '{print $1}'` [ -z $command ]&&command="-h" function helptext() { echo "getinterface.sh[-i interface|-I IP|-a|-h]" echo "-----------------------------------------" echo "-i interface (show ip of the interface)" echo "-I IP (show interface of the IP)" echo "-a list all interfaces and their IPs" echo "-h show the help text" echo "-----------------------------------------" } if [ $command == "-h" ];then helptext elif [ $command == "-i" ];then interface=$2 ifconfig $interface >/dev/null flag=$? if [ -z $interface ]; then echo "please input the interface" exit 2 elif [ $flag -ne 1 ];then ip=`ifconfig $interface | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'` echo "$interface $ip" else echo "$interface is not exist" fi elif [ $command == "-I" ];then IP=$2 if [ -z $IP ]; then echo "please input the IP" exit 2 else for inter in $list; do ip_temp=`ifconfig $inter | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'` if [ $IP == $ip_temp ];then echo "$IP : $inter" exit 0 fi done fi echo "$IP is not exist in the interfaces of the device" elif [ $command == "-a" ];then for inter in $list; do ip_temp=`ifconfig $inter | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'` if [ $inter != "lo" ];then echo "$inter:$ip_temp" fi done else echo "the format error!!!" helptext fi
相关文章推荐
- 解决adb shell input text 中文输入,unicode转utf-8
- 查看linux中shell的几种方法汇总
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
- 有关PowerShell脚本你必须知道的十个基本概念
- 有关PowerShell脚本你必须知道的十个基本概念
- PowerShell 在线教程 4
- PowerShell 在线教程 4
- PowerShell与Unix Shell对比:八大实例
- PowerShell与Unix Shell对比:八大实例
- bash的基础特性(1)
- Linux下简单Shell实现(三)获取任务列表
- shell编程
- shell-流程控制 if
- Linux默认shell—bash shell的优点
- shell常用特殊变量
- Linux下简单Shell实现(二)基本功能---主函数及init()函数
- HBase-1.0.1学习笔记(二)HBase Shell工具使用
- shell-字符处理命令/条件判断
- bash 变量