# check host value regex='^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$' if [ "$(echo $host | grep '[A-Za-z]')" != "" ]; then if [[ "$(host $host)" =~ "not found" ]]; then echo host $host not found exit 4 fi elif [[ ! $host =~ $regex ]]; then echo $host is an invalid host address exit 5 fi
This should explain the regex in variable(regex):
Regular Expression description
^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$
Options: ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character)
Match the regular expression below and capture its match into backreference number 1 - ([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
Match either the regular expression below (attempting the next alternative only if this one fails) - [1-9]
Match a single character in the range between "1" and "9" - [1-9]
Or match regular expression number 2 below (attempting the next alternative only if this one fails) - [1-9][0-9]
Match a single character in the range between "1" and "9" - [1-9]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 3 below (attempting the next alternative only if this one fails) - 1[0-9][0-9]
Match the character "1" literally - 1
Match a single character in the range between "0" and "9" - [0-9]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 4 below (attempting the next alternative only if this one fails) - 2[0-4][0-9]
Match the character "2" literally - 2
Match a single character in the range between "0" and "4" - [0-4]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 5 below (the entire group fails if this one fails to match) - 25[0-5]
Match the characters "25" literally - 25
Match a single character in the range between "0" and "5" - [0-5]
Match the regular expression below and capture its match into backreference number 2 - (\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}
Exactly 3 times - {3}
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. - {3}
Match the character "." literally - \.
Match the regular expression below and capture its match into backreference number 3 - ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
Match either the regular expression below (attempting the next alternative only if this one fails) - [0-9]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 2 below (attempting the next alternative only if this one fails) - [1-9][0-9]
Match a single character in the range between "1" and "9" - [1-9]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 3 below (attempting the next alternative only if this one fails) - 1[0-9][0-9]
Match the character "1" literally - 1
Match a single character in the range between "0" and "9" - [0-9]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 4 below (attempting the next alternative only if this one fails) - 2[0-4][0-9]
Match the character "2" literally - 2
Match a single character in the range between "0" and "4" - [0-4]
Match a single character in the range between "0" and "9" - [0-9]
Or match regular expression number 5 below (the entire group fails if this one fails to match) - 25[0-5]
Match the characters "25" literally - 25
Match a single character in the range between "0" and "5" - [0-5]
Assert position at the end of a line (at the end of the string or before a line break character) - $