Other Help Topics :: Set network settings on boot



Quote (friedgold @ May 06 2005,16:12)
Does any one know how the best way to check if a string is a valid ip address?

[ -z "$(echo "$i" | awk '/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/')" ]

should check the string is in the form ###.###.###.### where the #'s are nos, but it doesn't rule out stuff like 999.999.999.999.

Here is a neat way I saw using the cut command to parse out the string:

Code Sample
#!/bin/bash
# pass the ip string as an argument to this script

if echo $1 | grep -q "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$"
then
   for i in 1 2 3 4
   do
       export dotpart=` echo $1 | cut -f $i -d '.' `
       if [ $dotpart -lt 0 -o $dotpart -gt 255 ]
       then
           echo "ip address was bad"
           exit 1
       fi
   done
   echo "ip address was good"
else
   echo "ip address was bad"
   exit 1
fi
exit 0

Hey, I found my problem, well most of it anyway. This is really nice, since I have to find a way to get this to work with a frugal install and need network to come up automatically.

I will probably do some tweaking, but it looks good. Thanks for doing most of the hard work.


original here.