法一

root@test ~$eval `ifconfig | sed -rn '/^[^ \t]/{N;s/(^[^ ]*).*addr:([^ ]*).*/\1 \2/p}' | awk '$2!~/^127|^0|^$/{print $1"="$2}'|head -1`
root@test ~$
root@test ~$
root@test ~$echo $eth0
10.240.210.131

法二

root@test ~$echo `ifconfig | sed -rn '1,2'p | awk '{print $2}' | awk -F "addr:" '{print $2}' |sed /^\s*$/d`
10.240.210.131



获取公网IP


ifconfig|grep -oE 'inet addr:([0-9]{1,3}\.?){4}'|grep -oE '([0-9]{1,3}\.?){4}'|grep -vE '^(127|172|192|10)'