LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-30-2009, 09:25 AM   #1
texaganian
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
string matching in bash 2.0.5


OK, sorry, I know this is pretty basic but my brain doesn't want to wrap around it.

I need to create a bash shell script that tests the first parameter to see if it matches the following regex:
^[rRsS][0-9]{3}
Problem is, the box runs bash 2.0.5 and it doesn't have the regex match operator.

So what's the cleanest way to do this?
 
Old 11-30-2009, 09:39 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
1. "first parameter" of what?

2. What is the "regex match operator"?

3. Why not install more current BASH?

You can use that Regex with any number of utilities, including SED and GREP.
 
0 members found this post helpful.
Old 11-30-2009, 09:54 AM   #3
texaganian
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
1. "first parameter" of what?

2. What is the "regex match operator"?

3. Why not install more current BASH?

You can use that Regex with any number of utilities, including SED and GREP.
  1. I need to test the first argument passed to the script. i.e, $1.
  2. The regex match operator I was referring to is the =~ operator in bash 3. That would make it as easy as
    Code:
    if [[ "$1" =~ ^[rRsS][0-9]{3} ]]
    but...
  3. I can't do that because I don't manage the box so I can't change the installed version of bash.
 
Old 11-30-2009, 10:01 AM   #4
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
Bash Conditional Expressions do not include a "regex match operator".

Assuming the regular expression you gave means you want to match a string that begins with a character from rRsS followed by 3 characters from 0-9 ... here's an untested way to test $1 for it
Code:
case "$1" in
    [rRsS][0-9][0-9][0-9]* )
        echo 'Got it!'
esac
EDIT: but the [[ ]] detailed in Conditional Constructs does include =~

Last edited by catkin; 11-30-2009 at 10:04 AM.
 
1 members found this post helpful.
Old 11-30-2009, 10:34 AM   #5
texaganian
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Code:
case "$1" in
    [rRsS][0-9][0-9][0-9]* )
        echo 'Got it!'
esac
Perfect! I knew it was going to be pretty straightforward but my brain just wasn't cooperating!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 string matching Crafttype Linux - Newbie 2 05-26-2009 07:27 PM
Pattern matching in BASH. ccin1492 Programming 8 12-19-2008 12:00 PM
IPTables String Matching on FC5 jwhitehead Linux - Security 2 07-02-2006 01:42 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 03:39 AM
regular expresions: java: matching lines with out string zymos Programming 1 05-28-2005 10:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:35 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