前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell脚本-shell的getops 原

shell脚本-shell的getops 原

作者头像
阿dai学长
发布2019-04-03 11:04:59
7930
发布2019-04-03 11:04:59
举报
文章被收录于专栏:阿dai_linux阿dai_linux

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

  • 使用一下形式:getinterface.sh [-i interface | -I ip]
  • 当用户使用-i选项时,显示指定网卡的IP地址;当用户使用-I选项时,显示其指定ip所属的网卡。 例: sh getinterface.sh -i eth0 sh getinterface.sh -I 192.168.0.1
  • 当用户使用除[-i | -I]选项时,显示[-i interface | -I ip]此信息。
  • 当用户指定信息不符合时,显示错误。(比如指定的eth0没有,而是eth1时)
代码语言:javascript
复制
#!/bin/bash
##Find ip or interface
##Written by Adai 2017-09-06
read -p "Input 'i' to interface;'I' to ip: " c
if [[ $c == i ]]
then
    read -p "Please input your interface name: " n
    ip=`ifconfig | egrep -A1 "$n" |egrep -v "$n" |awk '{print $2}'`
    echo "ip is: $ip"
elif [[ $c == I ]]
then
    read -p "Please input your ip:" m
    ens=`ifconfig | egrep -B1 "$m" |egrep -v "$m"|awk -F ":" '{print $1}'`
    echo "interface is: $ens"
else
    echo "Unknown variate.Please check your option!"
fi

铭哥:

代码语言:javascript
复制
#!/bin/bash
ip add |awk -F ":" '$1 ~ /^[1-9]/ {print $2}'|sed 's/ //g' > /tmp/eths.txt
[ -f /tmp/eth_ip.log ] && rm -f /tmp/eth_ip.log
for eth in `cat /tmp/eths.txt`
do
    ip=`ip add |grep -A2 ": $eth" |grep inet |awk '{print $2}' |cut -d '/' -f 1`
    echo "$eth:$ip" >> /tmp/eth_ip.log
done

useage()
{
    echo "Please useage: $0 -i 网卡名字 or $0 -I ip地址"
}

wrong_eth()
{
    if ! grep -q "$1" /tmp/eth_ip.log
    then
        echo "请指定正确的网卡名字"
    exit
    fi
}

wrong_ip()
{
    if ! grep -qw "$1" /tmp/eth_ip.log
    then
        echo "请指定正确的ip地址"
    exit
    fi
}

if [ $# -ne 2 ]
then
    useage
    exit
fi

case $1 in
    -i)
    wrong_eth $2 
    grep $2 /tmp/eth_ip.log |awk -F ':' '{print $2}'
    ;;

    -I)
    wrong_ip $2
    grep $2 /tmp/eth_ip.log |awk -F ':' '{print $1}'
    ;;

    *)
    useage
    exit
esac

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/09/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档