<分区>
下面的程序应该读取并验证用户键入的 IP 地址。 IP 验证工作正常。我需要的是弄清楚条件(while/if&else)的设置,在输入无效 IP 地址的情况下提示用户重新输入 IP?
echo "Enter an IP address:"
read IP_ADDRESS
# Check if the format looks right
if echo "$IP_ADDRESS" | egrep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
then
#check that each octect is less than or equal to 255:
VALID_IP_ADDRESS="$(echo $IP_ADDRESS | awk -F'.' '$1 <=255 && $2 <= 255 && $3 <=
25&& $4 <= 255')"
## Here is the Pseudo code of what I am trying to achieve
# If the IP address is not VALID_IP_ADDRESS prompt the user to re-enter IP
#Pseudo Code not actaul
while [[ ! VALID_IP_ADDRESS]] do
read -p "Not an IP. Re-enter: " IP_ADDRESS
done
# Another way to I tried
if $VALID_IP_ADDRESS; then
echo "You have alright IP address!"
else
read -p "Not an IP. Re-enter: " IP_ADDRESS
fi