仅使用给定字符执行真实情况

问题描述 投票:0回答:1

大家好,问题是我需要一个脚本,在启动 skript 后向我的服务器添加一些信息 但它需要输入IP地址,但如果我没有以正确的形式输入IP,我也希望它向我发送一条错误消息

我尝试过的



while true ; do
   read -p "which IP did the Location used? in this form xx.xx.xx" newip
   case $newip in
       [0123456789.]* )echo "success";break;;
       * ) echo "only input in this Form xx.xx.xx";;
   esac
done

我想要的

该位置使用哪个 IP?采用这种形式 xx.xx.xx

输入10.10.10

输出成功

该位置使用哪个 IP?采用这种形式 xx.xx.xx

输入10-10.10

仅输出此表单 xx.xx.xx 中的输入

该位置使用哪个 IP?采用这种形式 xx.xx.xx

输入10.10.10

输出成功

linux bash do-while
1个回答
0
投票

在 glob 中,

*
匹配 any 字符。

Bash 确实有使用

=~
的正则表达式支持。例如:

if [[ $newip =~ ^[0-9]{2}\.[0-9]{2}\.[0-9]{2}$ ]]; then
    echo ok
else
    echo bad
fi
© www.soinside.com 2019 - 2024. All rights reserved.