Created
February 25, 2013 14:50
-
-
Save compor/5030293 to your computer and use it in GitHub Desktop.
check ipv4 validity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # the argument must in a dot decimal ipv4 format | |
| function is_valid_ipv4() { | |
| if [ -z "$1" ]; then | |
| return 0; | |
| fi | |
| IFS="." | |
| local isvalid=1 | |
| local bytectr=0 | |
| for byte in $1; do | |
| bytectr=$(( $bytectr + 1 )) | |
| # check if the 1st byte is greater than 0 | |
| if [ ${bytectr} -eq 1 ] && [ ${byte} -le 0 ]; then | |
| isvalid=0 | |
| break | |
| fi | |
| if [ ${byte} -lt 0 ] || [ ${byte} -gt 255 ]; then | |
| isvalid=0 | |
| break | |
| fi | |
| done | |
| if [ ${bytectr} -ne 4 ]; then | |
| isvalid=0 | |
| fi | |
| unset IFS | |
| return ${isvalid} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment