Script help

Jacques B. jjrboucher at gmail.com
Tue Feb 12 13:03:04 UTC 2008


On Feb 12, 2008 5:23 AM, Tom Brown <tom at ng23.net> wrote:
> Hi
>
> I am scripting some post install bits on a kickstart machine that
> downloads certain configuration files depending on the network the
> machine is in.
>
> I am trying to construct an if statement that will run something like
>
>
>
> ADDR=`ifconfig | grep "inet addr" | head -2 | awk '{print$2'} | cut -c
> 14,15`
>
> if [ "$ADDR" = "11" ] ; then
>         echo "downloading bar"
> fi
>
> if [ "$ADDR" = "12" ] ; then
>         echo "downloading foo"
>
> fi
>
> etc
>
> and there are 5 different configs - If none of the configs are found
> then it downloads a default - Am i going about this the right way?
>
> thanks

As Todd suggested, the case command will work well for this scenario.
Are you sure your ADDR= command is going to work as expected for
various scenarios?  I'm not sure of the reason for head -2, and the
cut command might not yield the desired results either depending on
the number of digits in the octets of your IPs.  Is there more than
one NIC in the machines? Also you are catching your loopback address
with the current scenario.  You could add a grep -v 127.0.0.1 to
eliminate the loopback.  Also instead of awk you can do the following
using sed and then pattern substitution.

ADDR=$(ifconfig | sed -e '/127.0.0.1/d' -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:192.168.0.1"
(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.




More information about the fedora-list mailing list