Skip to content

Instantly share code, notes, and snippets.

@honzahommer
Last active January 28, 2019 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzahommer/579b4fde8d43ab821b15a6527c73c901 to your computer and use it in GitHub Desktop.
Save honzahommer/579b4fde8d43ab821b15a6527c73c901 to your computer and use it in GitHub Desktop.
isipv4.sh
#!/usr/bin/env bash
isipv4() {
if [[ $# -eq 0 ]] ; then
return 1
fi
if [[ $# -gt 1 ]] ; then
local this="${FUNCNAME[0]}"
for i in "$@"; do
$this $i || return 1
done
fi
local ip=$1
local IFS=.
local -a a=($ip)
local quad
[[ $ip =~ ^[0-9]+(\.[0-9]+){3}$ ]] || return 1
for quad in {0..3}; do
[[ "${a[$quad]}" -gt 255 ]] && return 1
done
return 0
}
if [[ $# -ne 0 ]] ; then
isipv4 $@
exit $?
fi
#!/usr/bin/env bash
this=$(basename $0)
func=${this%".test.sh"}
file="$(dirname $0)/$func.sh"
if [ ! -f "$file" ]; then
echo "$file: File not found"
exit 1
fi
\. "$file"
if [[ $(type -t $func) != function ]]; then
echo "$func: Function not found"
exit 0
fi
while read -r test; do
$func $test || exit 1
done << EOF
1.1.1.1
10.0.0.0
EOF
while read -r test; do
$func $test && exit 1
done << EOF
1.1.1.
10.0.256.0
EOF
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment