[plug] getting IP address in a shell script

Leon Brooks leon at brooks.fdns.net
Tue Jul 30 18:55:32 WST 2002


On Tue, 30 Jul 2002 17:57, Craig Ringer wrote:
> Any ideas about a non-ugly (ie pref not /sbin/ifconfig eth0 | awk ... )
> method of getting the local machine's IP address into a variable in a
> shell script? There's probably something obvious I'm missing but apropos
> and google haven't helped on a quick glance.

Yeah. Just use 127.0.0.1 (-:

If you mean the first ethernet card's IP address, put the string...

    xdialog msgbox \
      $(ifconfig eth0|gawk '-F[ :]+' '/inet addr/ { print $4 }') 5 20

...behind an icon. (-:

If you want neat commands and don't care about the complexity of the gawk 
script behind them, paste this into a file called `ip':

BEGIN {
    FS="[ :]+"
}

/inet addr/ {
    print $4
}

...now do:

    xdialog msgbox $(ifconfig eth0|gawk -f ip) 5 20

BTW, the $(program) construct is a bashism, us `program` on other shells. I 
use it becuase it nests and is more readable.

Cheers; Leon



More information about the plug mailing list