I have a small piece of code which checks IP address validity :
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then
stat=1
else
stat=0
fi
fi
return $stat
}
But I am having problems with its usage in bash conditionals. I have tried many techniques to test its return value but most of them fail on me.
if [[ !$(valid_ip $IP) ]]; then
if [[ $(valid_ip IP) -eq 1 ]]; then
etc. etc. Can anyone suggest what should I do here ?
EDIT
Following your suggestions I have used something like :
if valid_ip "$IP" ; then
... do stuff
else
perr "IP: \"$IP\" is not a valid IP address"
fi
and I get errors like
IP: "10.9.205.228" is not a valid IP address