gpt4 book ai didi

linux - While 循环/if else 循环读取和验证 IP 地址

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:20 25 4
gpt4 key购买 nike

<分区>

下面的程序应该读取并验证用户键入的 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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com