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.
MY KERNEL VERSION:
# uname -r
2.6.16.60-0.21-bigsmp
I have a bash script that takes an IP-Address as it's first argument ( i.e. "$1" ).
I setup a REGEX check to make sure that it is actually in the correct format of an IP Address. (NO IPv6).
I have it check all 4 parts of the address like so:
1st) Can be either 2 -OR- 3 digits long 2nd) Can be 1 -TO- 3 digits long 3rd) Can be 1 -TO- 3 digits long 4th) Can be 1 -TO- 3 digits long ***) All are followed by a period "."
So basically the first part must be 2 or 3 digits long and the rest can be 1 to 3 digits long. All followed by
periods, except for the final one.
Here's my REGEX check:
Code:
shopt -s extglob
### Check IP Address is in correct format...
if [[ "$1" =~ ^[0-9]{2,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
echo "IP Address is in the correct format."
ipAddr=$1
fi
And I used the caret "^" and the dollar sign "$" so the match would be exact. But for some reason
if I enter the following as an IP-Address, then it passes as true... Not sure why since it is missing
a part of the address.
If I enter "192.168.1" as the IP, then it passes...? Anyone know why this is? I thought the "$" was supposed to prevent a partial match?
Any suggestions would be great.
Don't really need a different REGEX just confused and would like to know why MINE isn't doing what it should...?
Just for curiosity: which version of bash are you using mrm5102? For me it’s working in the original form you posted already without any extra assignment to a variable first:
Code:
$ bash --version
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
grail is basically correct above. When text is inserted directly, it is first parsed by the shell, and all shell-reserved characters have to be accounted for. This can be decidedly non-trivial to accomplish when it comes to regular expressions.
If a value is first stored in a variable, however, the value is only expanded after the majority of the parsing operations have finished. Only word-splitting and globbing take place after expansion, according to shell parsing order.
But since [[..]] is a special keyword, it doesn't do any processing of variable contents after expansion, meaning that nothing inside will be treated as special by the parser.
Thus the recommended way to handle regular expressions is to store them in a variable first. All you have to do is hard-quote the entire expression when you're setting the variable.
But remember, don't quote the variable when using it, or else the resulting string will be treated as a literal value.
Sorry it seems that I must have accidentally turned off email notifications for this thread...
Thanks for the suggestions!
Reuti,
The server is running on:
bash --version
GNU bash, version 3.1.17(1)-release (i586-suse-linux)
Copyright (C) 2005 Free Software Foundation, Inc.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.