Files
hpr_website/www/eps/hpr2669/hpr2669_bash12_ex5.sh

36 lines
686 B
Bash
Executable File

#!/bin/bash
#
# An IP address looks like this:
# 192.168.0.5
# Four groups of 1-3 numbers in the range 0..255 separated by dots.
#
re='^([0-9]{1,3}\.){3}[0-9]{1,3}$'
#
# The address is expected as the only argument
#
if [[ $# -ne 1 ]]; then
echo "Usage: $0 IP_address"
exit 1
fi
#
# Validate against the regex
#
if [[ $1 =~ $re ]]; then
#
# Look at the components and check they are all in range
#
for d in ${1//./ }; do
if [[ $d -lt 0 || $d -gt 255 ]]; then
echo "$1 is not a valid IP address (contains $d)"
exit 1
fi
done
echo "$1 is a valid IP address"
else
echo "$1 is not a valid IP address"
fi