I'm making a script that validate an IP address. I do this:
read pool checkIp() { 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 [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] stat=$? fi return $stat } checkIp $pool if [ $? -eq 0 ] then echo "valid" else echo "invalid" fi The problem is that now the requirement is to valid that from the 2nd octet the input can contain the wildcard "*" (i think is better to use CIDR notation but is not the case), and now i can't use -le because if there is not a number, it fails.
I'd tried some forms but all conversions fails.
010.010.010.010could cause confusion as most applications and library functions (gethostbyname, inet_addr...) consider it the same as8.8.8.8while some other the same as10.10.10.10(inet_pton).