获取某个接口的IP,用于conky中显示

#!/bin/bash
#show ip and netmask for specific device

if [ -z $1 ];then
    interface="eth0"
else 
    interface=$1
fi

ifconfig $interface &> /dev/null || {
    echo "Usage: `basename $0` [eth0(default),eth1......]"
    echo "device $interface not found"
    exit 1
}

temp=`ifconfig $interface |grep 'inet[^6]'`

ip=`echo $temp|sed -e s/inet\ //g -e s/\ netmask.*$//g`
netmask=`echo $temp|sed -e s/.*netmask\ //g -e s/\ broadcast.*$//g`
#echo $ip/$netmask
echo $ip

archlinux的ifconfig版本较新.输出的格式不同.

你可能感兴趣的:(获取某个接口的IP,用于conky中显示)