ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
function convert {
while read ip; do
if ! [[ "$ip" =~ [^0-9.-] ]];
then
todo[$y]+=" $ip $ip"
if [ $z -eq 2000 ]
then
z=0
let y=$y+1
else
let z=$z+1
fi
fi
done
dynamic_objects -do ti_$type
dynamic_objects -n ti_$type
for i in "${todo[@]}" ;
do
dynamic_objects -o ti_$type -r $i -a
done
}
Your ipv6 addresses contain characters not covered by your regex - hexadecimal digits and the colon separator.
Add those to your regex and it will work such as it is.
Of course, it will also match many things other than IP addresses as well and will not differentiate ipv4 from ipv6, but those are not mentioned as considerations so may not be important to you.
It is probably important that you at least consider a few patterns you do not want to match in order to help specify your regex more precisely. For example, your existing regex will also match phone numbers in almost any format, and with the addition of hex digits it will match many other things including "deadbeef" and 1-800-bad-deed, among a great many other things!
line 11: syntax error in conditional expression: unexpected token `('
line 11: syntax error near `^(['
line 11: `if [[ ".$ip" =~ ^([.][0-9]{1,3}){4}$ ]] || [[ ":$ip" =~ ^(:[0-9a-fA-F]{,4}){4,8}$ ]];'
Code:
function convert {
while read ip; do
# if ! [[ "$ip" =~ [^0-9.-] ]];
if [[ ".$ip" =~ ^([.][0-9]{1,3}){4}$ ]] || [[ ":$ip" =~ ^(:[0-9a-fA-F]{,4}){4,8}$ ]];
then
todo[$y]+=" $ip $ip"
if [ $z -eq 2000 ]
then
z=0
let y=$y+1
else
let z=$z+1
fi
fi
done
dynamic_objects -do ti_$type
dynamic_objects -n ti_$type
for i in "${todo[@]}" ;
do
dynamic_objects -o ti_$type -r $i -a
done
}
When using Bash regex, predefine the patterns as variables with single-quotes around them, and then use those variables in the if statement.
Also, note that "::1" is a valid IPv6 address, which is not matched by that pattern, and I just noticed that the IPv4 pattern is requiring a "." at the start, which is just wrong.
The IPv6 pattern below may or not be good enough for your needs (I don't see any mention of what those are).
Likewise, IPv4 only allows each octet to be up to 255 - you may or not need separate validation to check that.
Code:
IPv4='^[0-9]{1,3}(\.[0-9]{1,3}){3}$'
IPv6='^[0-9A-Fa-f:]{3,39}$'
if [[ "$ip" =~ $IPv4 || "$ip" =~ $IPv6 ]]
then
...
fi
Almost only counts in horseshoes and hand grenades.
Certainly not regex. Precision, as in exact specification, is absolute. The more you use it, the more you see the truth in this. You can only learn it by doing.
When using Bash regex, predefine the patterns as variables with single-quotes around them, and then use those variables in the if statement.
Also, note that "::1" is a valid IPv6 address, which is not matched by that pattern, and I just noticed that the IPv4 pattern is requiring a "." at the start, which is just wrong.
The IPv6 pattern below may or not be good enough for your needs (I don't see any mention of what those are).
Likewise, IPv4 only allows each octet to be up to 255 - you may or not need separate validation to check that.
Code:
IPv4='^[0-9]{1,3}(\.[0-9]{1,3}){3}$'
IPv6='^[0-9A-Fa-f:]{3,39}$'
if [[ "$ip" =~ $IPv4 || "$ip" =~ $IPv6 ]]
then
...
fi
Yep that seems to have worked!! Thanks for the help
I am testing it further let me see. Hopefully should not encounter any other issues.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.