Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created January 8, 2014 20:40
Show Gist options
  • Save coderofsalvation/8324205 to your computer and use it in GitHub Desktop.
Save coderofsalvation/8324205 to your computer and use it in GitHub Desktop.
returns 0 if parameter is a valid ip4 address, non-zero otherwise
# 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