Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Bash scripting, use regex as a case pattern

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Closed Thread
 
Thread Tools Display Modes
Old 07-18-2006   #1 (permalink)
Just Joined!
 
Join Date: May 2004
Posts: 2
Bash scripting, use regex as a case pattern

Hi, referring to this syntax of case:
case word in
[(]pattern1) compound-list;;
esac

when pattern1 is a regular expression with expressions grouped using parentheses like in:
(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))\{3\}

an error occours, probably due to the ')' after the regular expression.
-bash: ./badscript: line 120: syntax error near unexpected token `25[0-5]'
-bash: ./badscript: line 120: ` (25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))\{3\} )'

How fix it?

Thanks
paolodina is offline  


Old 07-18-2006   #2 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 817
Hi, paolodina.

I have no immediate solution for you without some thought, but the expressions used in the case statement are essentially pathname expressions (such as you use on the command line), not regular expressions:
Code:
       case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
              A case command first expands word, and tries to match it against
              each pattern in turn, using the same matching rules as for path-
              name  expansion (see Pathname Expansion below).  When a match is
              found, the corresponding list  is  executed.   After  the  first
              match,  no subsequent matches are attempted.  The exit status is
              zero if no pattern matches.  Otherwise, it is the exit status of
              the last command executed in list -- man bash

You may need to do some processing with sed if you want to involve regular expressions in a case statement.

Perhaps our colleagues have some more creative ideas ... cheers, drl
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline  
Old 05-10-2009   #3 (permalink)
Just Joined!
 
Join Date: Jan 2007
Posts: 1
case $name in
[ab]{2,3} )
echo do stuff
;;
* )
;;
esac
skog is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:22 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2