Hi all,
Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh?
Thanks,
Marc
Hi all,
Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh?
Thanks,
Marc
Well, I saw vino's reply only now that I've written my own function
I post my code anyway:
IP="123.246.189.235"
TEST=`echo "${IP}." | /usr/xpg4/bin/grep -E "([0-9]{1,3}\.){4}"`
if [ "$TEST" ]
then
echo "$IP" | nawk -F. '{
if ( (($1>=0) && ($1<=255)) &&
(($2>=0) && ($2<=255)) &&
(($3>=0) && ($3<=255)) &&
(($4>=0) && ($4<=255)) ) {
print($0 " is a valid IP address.");
} else {
print($0 ": IP address out of range!");
}
}'
else
echo "${IP} is not a valid IP address!"
fi
Is tested on Solaris, you may have to change the grep invocation according to your platform (grep, grep -E, egrep, ...).
thanks vino and robotronic