IP address variable?

Paul F. Almquist paul at almquist.name
Wed Feb 16 13:45:33 UTC 2005


On Monday 14 February 2005 16:41, Deron Meranda wrote:
> 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.
I heartily agree.  I have seen the backtick frequently misprinted in books and 
on the net, and of course easily misread. Another advantage to the $(command) 
notation is that it can be nested, e.g.
  $(command1 $(command2))
while backticks cannot.

-- 
Paul F. Almquist
paul at almquist.name
Eau Claire, WI  USA




More information about the fedora-list mailing list