Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
I have a script which gets the IP address from a web site (Eg. http://checkip.dyndns.org among others) and strips everything (Eg. "Your IP is:") leaving just the IP - that part works fine. I have a problem with error checking that a valid IP address was returned.
What I've been trying to do is run a sed command that matches the following:
A 0, 1, or 2, followed by 0-9 appearing either 0, 1, or 2 times, followed by a '.'. Then repeating that 3 times to match a 4 number IP address. Perhaps not perfect logic as that would validate 0.0.0.0 - but I can be fairly sure that if the getting the IP from a web site fails then it's not going to leave me with anything approximating an IP which might cause my error check to give a false positive.
Here's an example of my (not working) sed command:
Code:
echo "255.1.22.33" | sed "s/^[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}\.[0-2]{1,1}[0-9]{0,2}$//g"
My test is to run the sed command on what should be the IP with the sed replace section empty (as above), therefore if sed provides an empty string I know that the IP was valid.
The problem is that the code above just returns the whole IP given in the echo. I've been pulling my hair out trying to fix it but can't work it out at all.
The first problem is that you probably need to escape your braces with a backslash: '\{1,1\}'. I am not sure that this is true in all implementations, locations, and other such variations, so you should test it with something simple.
The second problem is that the first digit of '33' does not fall into the range [0-9], but your regular expression is expecting this. Here is a technique for finding numbers in a range.
The first problem is that you probably need to escape your braces with a backslash: '\{1,1\}'. I am not sure that this is true in all implementations, locations, and other such variations, so you should test it with something simple.
Yes that has fixed it - I hadn't realized that could be a problem.
Quote:
Originally Posted by Wellesly
The second problem is that the first digit of '33' does not fall into the range [0-9], but your regular expression is expecting this. Here is a technique for finding numbers in a range.
Actually the '33' was just a mistake in my post here (and you meant the range [0-2]). The actual code was using my real IP address and at the point of posting I changed it stupidly to an invalid IP. Thanks for the number range link, I'll try that approach instead.
Thanks again and happy new year.
EDIT: Of course my logic was all wrong anyway and I suspect a complete brain meltdown due to New Year excess. 33 is valid in an IP, only 333 would not be, an IP only need begin with 0,1,2 if it's 3 digits in length, if it's 1 or 2 digits in length it can begin with anything. Oops. END EDIT
i guess it comes a little late for solving your problem, but just in case someone else is looking for it, here's a solution a friend of mine and me came up with:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.