#!/bin/bash# Name: optind.sh# Description:# Author:# Version: 0.0.1# Date: 2016-05-12 10:02:22# Usage:show_ip() {# judge whether eth exists.if ! ifconfig | grep -o "^[^[:space:]]\{1,\}" | cut -d: -f 2 | grep $1 &> /dev/null; thenreturn 1fiecho -n "$1: "ifconfig $1 | grep -o "inet addr:[0-9\.]\{7,\}" | cut -d: -f 2}show_eth() {# judge whether ip exists.if ! ifconfig | grep -o "inet addr:[0-9\.]\{7,\}" | grep $1 &> /dev/null; thenreturn 2fiecho -n "$1: "ifconfig | grep -B 1 $1 | grep -o "^[^[:space:]]\{1,\}"}usage() {echo "getinterface.sh [-i inerface| -I ip]"}case $1 in-i | -I)while getopts ":i:I:" opt;docase $opt ini)show_ip $OPTARG[ $? -ne 0 ] && echo "wrong ethenet card";;I)show_eth $OPTARG[ $? -ne 0 ] && echo "wrong ip";;*)usage;;esacdone;;*)usage;;- esac
#!/bin/bash# Name: getinterface.sh# Description:# Author:# Version: 0.0.1# Date: 2016-05-12 10:02:22# Usage:show_ip() {# judge whether eth exists.#修改1if ! ifconfig | grep -o "^[^[:space:]]\{1,\}" | cut -d: -f 2 | grep -E "\b$1\b" &> /dev/null; thenreturn 1fi# print ipecho -n "$1: "ifconfig $1 | grep -o "inet addr:[0-9\.]\{7,\}" | cut -d: -f 2}show_eth() {# judge whether ip exists.#修改2if ! ifconfig | grep -o "inet addr:[0-9\.]\{7,\}" | grep "$1\b" &> /dev/null; thenreturn 2fiecho -n "$1: "ifconfig | grep -B 1 $1 | grep -o "^[^[:space:]]\{1,\}"}usage() {echo "getinterface.sh [-i interface| -I ip]"}case $1 in-i | -I)while getopts ":i:I:" opt;docase $opt ini)show_ip $OPTARG[ $? -ne 0 ] && echo "wrong ethenet card";;I)show_eth $OPTARG[ $? -ne 0 ] && echo "wrong ip";;*)usage;;esacdone;;*)usage;;esac
本文出自 “hiyang” 博客,请务必保留此出处http://hiyang.blog.51cto.com/10728919/1775146
原文地址:http://hiyang.blog.51cto.com/10728919/1775146