LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-01-2011, 07:19 AM   #1
rrije
Member
 
Registered: Jul 2010
Distribution: openSUSE 11.4
Posts: 33

Rep: Reputation: Disabled
bash: regexp in 'if' doesn't evaluate correctly


Hi,

I'm using an 'if' statement to check whether one of positional arguments is a word or a construction like "x-y", but something doesn't seem right.

Here is the relevant part of the code:
Code:
if [[ "$3" =~ [a-Z]\-[a-Z] || "$3" =~ [a-Z] ]]; then 
grepRange=$3; else grepRange=$4
fi
And for the input like this:
Code:
bash -x script --list free fornax
it outputs this:
Code:
+ [[ fornax =~ [a-Z]-[a-Z] ]]
+ [[ fornax =~ [a-Z] ]]
+ grepRange=fornax
but shouldn't the $grepRange variable be blank in this case?

Bash version is 4.1.7(1)

Is there something I'm missing? Enclosing regexp in quotes or double quotes doesn't help.

Thanks in advance.
 
Old 03-01-2011, 07:23 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nope. The second regexp is matched:
Code:
[[ fornax =~ [a-Z] ]]
so that according to your code grepRange is $3. Please, can you better explain what you're trying to achieve?
 
Old 03-01-2011, 07:44 AM   #3
rrije
Member
 
Registered: Jul 2010
Distribution: openSUSE 11.4
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks!

I've adjusted it to ^[a-Z]$ and it works okay.

The script itself cats a file and breaks the list down into sections according to the first letter of a line.
 
Old 03-01-2011, 08:28 AM   #4
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
If only interested in the first letter, why not use that as the test:
Code:
if [[ ${3:0:1} =~ [a-Z] ]] ...
 
Old 03-01-2011, 09:52 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by grail View Post
If only interested in the first letter, why not use that as the test:
Code:
if [[ ${3:0:1} =~ [a-Z] ]] ...
If I understood the OP correctly then he only wants the branch to execute if $3 consists of one single letter.

@OP: You probably can also use this RegEx for both your cases:
Code:
if [[ "$3" =~ ^[a-Z](-[a-Z])?$ ]];then
and get rid of the extra OR comparison. Works with my bash version 4.1.5.
 
Old 03-01-2011, 12:44 PM   #6
rrije
Member
 
Registered: Jul 2010
Distribution: openSUSE 11.4
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks for the replies everyone.

Allow me to elaborate a bit on a purpose of the script:
There is a plaintext file containing a number of entries (several files, actually); I need to be able to list these entries by filename (or everything at once) and by letter (or several letters).

Here is a part of the code (screenshot, for syntax highlighting) and exemple output; it's a mess, but as a beginner I figured you have to start somewhere. I'd be grateful for any tips you could give.
 
Old 03-01-2011, 11:45 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
Well on a cursory look, my main thought is:

Try to look at the places where you have multiple pipes, especially when it is multiple pipes to / through the same application:
Code:
ls ... | grep ... | grep ... | sed ... | xargs ... | grep ... | sort ...
I would guarantee this could lose at least 3 of the pipes if not more with a little investigation.
 
  


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
deleting a line matching two or more regexp in bash, sed maybe? patolfo Programming 21 05-21-2010 12:30 PM
[SOLVED] BASH Script, 'awk' and 'if' question. lupusarcanus Linux - Newbie 7 03-14-2010 11:07 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM
Evaluate my Bash script for runaway proc RaelOM Programming 2 10-26-2007 09:30 AM
Bash-programming: RegExp? theonebeyond Programming 9 12-08-2004 02:26 AM

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

All times are GMT -5. The time now is 07:53 PM.

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