Re: IP address variable?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Monday 14 February 2005 16:41, Deron Meranda wrote:
> On Mon, 14 Feb 2005 14:13:15 -0800, paul <paul.hamradio@xxxxxxxxxxx> 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@xxxxxxxxxxxxx
Eau Claire, WI  USA


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux