Script help

Tom Brown tom at
Tue Feb 12 15:37:48 UTC 2008

Jacques B. wrote:
> <snip>
>> 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
> differently.
> Jacques B.

thanks for the pointers all - this was being tested on a box that has 
more than 1 NIC hence the reason for the cut.

I need to get the third octect of the NIC address and depending on what 
that is a different automounter file is installed and if no network is 
recognised then a default is downloaded. I hope that clears up what i am 
trying to do and i will look at the options.

More information about the fedora-list mailing list