LinuxQuestions.org
Help answer threads with 0 replies.
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 04-27-2006, 11:01 AM   #1
King V
Member
 
Registered: Oct 2001
Location: New Jersey
Distribution: Mandrake 10.2
Posts: 75

Rep: Reputation: 15
Pattern matching with a "case" statement in BASH


Ok, I'm not entirely sure what I'm doing wrong here, or if it's just a limitation of the scripting language, but I'm having a bit of trouble trying to match strings in a particular circumstance.

Let's say I'm going to compare a string variable to a string that has the text "mytext", but it can have zero or more spaces preceeding it, and zero or more spaces following it, and it must still match.

that is, the following should match (braces are used just to show where the string begins and ends, and are not actually part of the string):

[mytext]
[ mytext]
[ mytext ]
[mytext ]

etc.. but there can be more than one preceeding or appending spaces.

Now, with the case statement, I've tried to use the following:

case "$thestring" in

[\ ]*mytext[\ ]*) do_something;;
*) do_nothing;;
esac


.. but there's no match when there's ZERO spaces before or after the text, only if there's at least one.

Is there any way that I'm missing that I can use to do this with a single entry in the case, or am I absolutely required to do something as follows:

case "$thestring" in

mytext) do_something;;
mytext[\ ]*) do_something;;
[\ ]*mytext) do_something;;
[\ ]*mytext[\ ]*) do_something;;
*) do_nothing;;
esac
 
Old 04-27-2006, 01:29 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
with the brackets [] you are implying that you always have at least one of the characters there, in your case, just a space.
 
Old 04-27-2006, 01:31 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
looking a bit more the * after the brackets should help you out, I'll have another look
 
Old 04-27-2006, 01:51 PM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
It would be easier to strip any whitespace off before you test the string. Unfortunately, bash's syntax is crap and doesn't support nested expansion, so you'll need to use a tmp variable, e.g.

Code:
tmp="${thestring# }"
thestring="${tmp% }"
case "$thestring" in
...

Last edited by ioerror; 04-27-2006 at 01:54 PM.
 
Old 04-27-2006, 02:50 PM   #5
King V
Member
 
Registered: Oct 2001
Location: New Jersey
Distribution: Mandrake 10.2
Posts: 75

Original Poster
Rep: Reputation: 15
Question

I think I'd have to loop those first two lines, because they'd only take the first leading and first trailing space from the string. At that point, I'm thinking the multiple-case entries might be a touch more efficient than looping.

If there were more than one space before and/or more than one space after, it wouldn't work as-is, if I'm reading it right.

I think I might just go with the 4-cases variant, since the asterisk after the [] doesn't seem to do what I thought it would.

On the other hand, if I knew that there'd only be zero or one spaces, then would the following work (without the use of a temporary variable):
Code:
thestring="${thestring# }"
thestring="${thestring% }"
case "$thestring" in
...

(the killer though is whether or not I can be assured of no more than one leading or trailing space)
 
Old 04-27-2006, 03:09 PM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Code:
#!/bin/sh

thestring=" mytext"

newstring=`echo $thestring | sed 's/\ //g'`

case "$newstring" in


    mytext) echo something;;
    *) echo nothing;;

esac
 
Old 04-27-2006, 03:44 PM   #7
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
wouldn't ${test//\ /} do what you want? Strips all spaces.
Code:
$ ~: test="    php      jpg        "
$ ~: echo ${test//\ /}
phpjpg
 
Old 04-27-2006, 03:47 PM   #8
King V
Member
 
Registered: Oct 2001
Location: New Jersey
Distribution: Mandrake 10.2
Posts: 75

Original Poster
Rep: Reputation: 15
Lightbulb

crabboy: I like that one.

Doing some time-testing, it's actually slower than checking the four-cases, but I like your method. It's more elegant-looking (ok, at least *I* think so).

EDIT: Ugh, but then I just realized something (thanks to seeing muha's example, actually) . . in the event that there's spaces in the midst of the string, it'll nuke those, too, much like muha's example. Bummer. For my needs, there *shouldn't* be spaces in the midst of the text, but again, it's one of those things where I can't absolutely be assured of that.

Last edited by King V; 04-27-2006 at 03:53 PM.
 
Old 04-27-2006, 04:19 PM   #9
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Quote:
(the killer though is whether or not I can be assured of no more than one leading or trailing space)
Bugger, misread your original post, thought there was only 1 space. If this were my script, I'd use

Code:
${${thestring# ##}% ##}
...but that'll only work in zsh.

Bash is a piece of crap when it comes to manipulating variables, so I think crabboy's sed suggestion might be the way to go here. Just add a 's/ *$//g' to handle trailing spaces (don't forget, you need to use -e if you pass more than one expression to sed).

Last edited by ioerror; 04-27-2006 at 04:21 PM.
 
  


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: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
bash equivalence of tcsh "alias em "emacs \!:1 &""? rgiggs Slackware 3 07-29-2004 02:07 AM
Bash script problems with "if" statement adz Programming 2 05-22-2004 03:34 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 10:43 AM

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

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