# to check if an input is a legal IP number
# A legal IP number is such as xxx.xxx.xxx.xxx
# where xxx is 0-255 and can be one to three digits long.
# And must begin and end with 0-255
# the regular expression of one segment should be
# '[01]?[[:digit:]]?[[:digit:]]|2[0-4][[:digit]]|25[0-5]'
echo "$1" | egrep '^([01]?[[:digit:]]?[[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([01]?[[:digit:]]?[[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([01]?[[:digit:]]?[[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([01]?[[:digit:]]?[[:digit:]]|2[0-4][[:digit:]]|25[0-5])$' > /dev/null
if [ $? == 0 ]; then
echo "The input '$1' is a legal IP address."
else
echo "The input '$1' is not a legal IP address."
fi
学校老师发的代码