如题,原脚本中判断的是传入的第一个参数$1
,这里我改成了IPA
,然后动态赋值,输出的 IPA 值也确实为 v6 ,但是判断为 v4 。请大家帮忙看看是哪里出错了
#!/bin/sh
IPA=`curl 'https://api64.ipify.org?format=json' | jq -r .ip`
echo $IPA
if [ "$IPA" != "${1#*:[0-9a-fA-F]}" ]; then
echo IPv6
elif [ "$IPA" != "${1#*[0-9].[0-9]}" ]; then
echo IPv4
else
echo "Unrecognized IP format '$1'"
fi
执行后的输出:
root@testbench:/home/testbench/userdata# ./ipv6.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 47 100 47 0 0 48 0 --:--:-- --:--:-- --:--:-- 47
2409:8a66:b53:c761:c3be:b1b3:69d7:fb01
IPv4
1
tlze 2022-02-22 19:30:58 +08:00 via Android 1
$IPA" != "${1#*:[0-9a-fA-F]}
把 1 改 |
2
tlze 2022-02-22 19:33:07 +08:00 via Android 1
要把 1 都改了。$IPA" != "${IPA#*:[0-9a-fA-F]}
下面的同 |
3
tlze 2022-02-22 19:34:25 +08:00 via Android 1
其他也要改。
sorry, 我手机有问题。 |
4
szdosar 2022-02-23 08:34:40 +08:00
供参考
[code] #!/bin/sh IPA=`curl https://api64.ipify.org` echo $IPA if [ "$IPA" != "${IPA#*:[0-9a-fA-F]}" ]; then echo IPv6 elif [ "$IPA" != "${IPA#*[0-9].[0-9]}" ]; then echo IPv4 else echo "Unrecognized IP format '$IPA'" fi [/code] |
5
exqibao 2022-02-23 17:01:57 +08:00
#!/bin/bash
IPA=`curl -s 'https://api64.ipify.org/'` echo $IPA if [[ $IPA =~ : ]];then # 包含:,则 IPV6 echo 'IPV6' elif [[ $IPA =~ . ]];then # 包含.,则 IPV4 echo 'IPV4' else echo "Unrecognized IP format '$IPA'" fi exit 0 |