LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2012, 12:16 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Rep: Reputation: 3
Bash Script - Check Argument with REGEX


Hello All,

Code:
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...?


Thanks in Advance,
Matt
 
Old 03-26-2012, 12:58 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try storing the regex in a variable with single quotes ... usually does the trick.
 
1 members found this post helpful.
Old 03-26-2012, 01:20 PM   #3
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
grail,

Hey, thanks for the reply... I'll give that a try and post back.

Thanks,
Matt
 
Old 03-26-2012, 01:32 PM   #4
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Hey grail,

SUCCESS!!! Great thanks for that, it worked perfectly!!

How come it needed to be in a variable..?

Thanks Again,
Matt
 
Old 03-26-2012, 01:59 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Are you sure that the check you described will work? Wouldn't it accept 800.512.121.2 as a valid URL?

I believe that the values in the URL are intended to represent octal numbers in the 0-255 (x00-xFF) range . . .
 
Old 03-26-2012, 10:37 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
function ck_ip_address {

    local array oIFS retval

    retval=1

    if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        oIFS=$IFS
        IFS='.'
        array=( $1 )
        IFS=$oIFS
        [[ ${array[0]} -le 255 && ${array[1]} -le 255 && ${array[2]} -le 255 && ${array[3]} -le 255 ]]
        retval=$?
    fi  

    return $retval

}  # end of function ck_ip_address
 
Old 03-26-2012, 11:57 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
How come it needed to be in a variable..?
My understanding is that some items will need to be escaped if used directly but once placed in a variable it is read in place.

A reference is here
 
Old 03-27-2012, 06:34 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
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.
 
Old 03-27-2012, 10:43 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 04-10-2012, 10:07 AM   #10
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 165

Original Poster
Rep: Reputation: 3
Hey Guys,

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.



Thanks Again,
Matt
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script argument disappears wahming Linux - General 4 10-26-2010 05:03 AM
execute script shell with regex as argument eguider Linux - Newbie 2 12-15-2009 07:26 PM
perl pass regex check as an argument lord-fu Programming 2 08-29-2007 02:55 PM
BASH: rename an argument of the script sylvaticus Programming 2 05-21-2007 08:20 AM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration