ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
...
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)
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.
(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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.