#!/bin/bash
#                      /usr/local/bin/valid_ip4
# http://crystalfaeries.net/posix/bin/valid_ip4
# celeste:crystalfaery 2015-10-24 23:57:51+00:00
# http://www.commandlinefu.com/commands/view/14897/checking-if-an-ip-is-valid.
let result=0	# default no error
until [ $# = 0 ]
do
	ip_to_test="$1"	# consume the next string argument
	shift

	# test as an IP
	echo "$ip_to_test" | egrep '^(([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$' >& /dev/null
	let test_result=$?	# save the status
	if [ $test_result = 0 ]
	then
		echo "$ip_to_test" >&1	#   valid as an IP address is echoed to STDOUT
	else
		let result=$test_result	# save most recent error for exit status
		echo "$ip_to_test" >&2	# invalid as an IP address is echoed to STDERR
	fi
done
exit $result	# a non-zero return indicates at least one argument was not a valid IP address


syntax highlighted by Code2HTML, v. 0.9.1