Created
January 8, 2014 20:40
-
-
Save coderofsalvation/8324205 to your computer and use it in GitHub Desktop.
returns 0 if parameter is a valid ip4 address, non-zero otherwise
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
# returns 0 if parameter is a valid ip4 address, non-zero otherwise | |
function isvalidip4() | |
{ #return 0 if parameter is a valid ip4 address, non-zero otherwise | |
#https://groups.google.com/forum/#!original/comp.unix.shell/NDu-kAL5cHs/7Zpc6Q2Hu5YJ | |
[ -z "$1" ] && return 1 | |
case "$*" in | |
""|*[!0-9.]*|*[!0-9]) return 1 ;; | |
esac | |
OLDIFS="$IFS" | |
IFS=. | |
set -- $* | |
IFS="$OLDIFS" | |
[ $# -eq 4 ] && | |
[ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] && | |
[ ${3:-666} -le 255 ] && [ ${4:-666} -le 254 ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment