IP address variable?

Deron Meranda deron.meranda at gmail.com
Mon Feb 14 22:41:04 UTC 2005


On Mon, 14 Feb 2005 14:13:15 -0800, paul <paul.hamradio at verizon.net> wrote:
> >>  > > I need to create a variable (or read an existing one) that returns
> >> the IP > address for eth0.
> >>  > I used to do it like this under RH 7.2:
> >>  > > IPADDR='ifconfig eth0|grep inet|cut -d ":" -f2|cut -d " " -f1'

Or another way that's perhaps a bit more robust under Linux (it
handles secondary IP addresses, scoped sub-routing tables, etc)

   DEVICE=eth0

   IPADDR=$(/sbin/ip addr show dev $DEVICE scope global primary | \
     awk '$1=="inet" {print $2}' | \
     cut -d/ -f1)

use "inet6" if you want IPv6 addresses instead.

Also, I find it more readable to use the bash $(xxxx) shell syntax
rather than backticks `xxxxx`.  You can do that even for
you ifconfig solution.

-- 
Deron Meranda




More information about the fedora-list mailing list