Validating IP address.

Hi All,
This is a small snippet that I am using to validate the format of IP address.This is working fine.
Since this is not elegant, please let me know if anyone has a better working code than the one below.

Also I am planning to ping the ip address given by the user and check the $? variable. Also let me know if anyone has a better way out, to check if the ip address is on the network.

Thanks!
nua7

IP_ADDR_VAL=$(echo "$ipadd" | grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')
if [ $IP_ADDR_VAL -eq 0 ]; then
    echo -e "*** Bad ip address: $ipadd\nTry again with correct IP Address.\nNo data has been entered into DNS."
    exit 2
fi  

As a stylistic issue, I would dissuade from the use of grep -c and [ xxx -eq 0 ] -- especially on $? -- in favor of something like

if ! echo "$ipadd" | grep -E '...' >/dev/null; then
  echo -e "*** Bad ..." >&2
  exit 2
fi

Also note the redirection of the error message to file descriptor 2. You might want to add $0 to the beginning of the error message as well.

The use of {1} in the regular expression is obviously redundant. In order to reduce backtracking, you might want to use [01][0-9]{0,2} to cover all strings beginning with 0 or 1 in one go, aand adjust the subsequent expression accordingly.

Note that this regular expression validates the format of an IPv4 address but does not work for an IPv6 address.

Thanks era and fpmurphy for all the suggestions!