Script help

Jacques B. jjrboucher at
Tue Feb 12 13:08:27 UTC 2008

> ADDR=$(ifconfig | sed -e '/' -n -e '/inet addr/p') # ADDR
> will contain all the lines with "inet addr" except the loopback.
> ADDR=${ADDR%% Bcast*} # output will contain "inet addr:"
> (or whatever your IP is of course)
> ADDR=${ADDR##*.} # ADDR will contain the last octect in the IP, "1" in
> the above scenario
> case $ADDR in
>     11 ) echo "downloading bar";;
>     12 ) echo "downloading foo";;
>     13 ) echo "downloading whatever";;
>       * ) echo "Unknown network.  Did not match any of the choices.";;
> esac
> Note that the above works for a scenario where there is only one NIC
> activated.  I haven't tested it with multiple NICs.
> Jacques B.

Further to that last message, I re-ran it without the
portion of the sed command and it still worked.  Reason being is that
the result of the sed is saved to the variable as a single string (so
the inet addr for eth0 followed by the inet addr line for the
loopback).  So when you do the pattern machine on the next line, only
the very first NIC is retained.  So the script is only suitable if you
only have to worry about the first NIC.  And like I said, the sed
command could be as below and it will function the same.
sed -n -e '/inet addr/p'

If you need to deal with multiple NICs where you are interested in a
NIC other than the first one listed, you'll have to script this

Jacques B.

More information about the fedora-list mailing list