我正在编写一个 bash 脚本,它会要求输入(在本例中是 IP 地址),但如果 IP 地址格式不正确,我希望它一直询问,直到用户正确输入。我问的是当 bash 没有 GOTO 命令时如何执行 GOTO。最简单的方法是什么?
我的代码是:
#!/bin/bash
# There will be additional code here that is not relevant to this question.
# Ask for IPv4 address
echo Please type the IPv4 address of the host:
read ipv4addr
if [[ $ipv4addr =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# do something
else
echo "The IP address is not in the correct format. Please try again."
# I need to have it go back to #Ask for the IPv4 address
fi