test for a pattern in sh?
David N. Lombard
dnl at speakeasy.net
Sun Oct 11 18:46:52 UTC 2009
stan wrote:
> I need to do something like this:
>
> if $VAR does not contain SV do something
>
> I found some examples for patterm`n matching with case, but I'd reaher use
> an if here, if I can do pattern matching with it.
>
> Is this possible?
First, use Bash not Bourne Shell. If you really do mean Bourne, and I
doubt you do, then you'll need to learn about expr(1). expr(1) is
something dinosaurs like me are quite expert in, but also understand
there's simply no reason to use such limited forms when bash (Korn) let
you do so much better.
This is the literal translation of what you asked:
#!/bin/bash
[[ $VAR != *SV* ]] && dosomething
If something is more than one line, try this:
#!/bin/bash
[[ $VAR != *SV* ]] && {
dosomething
dosomethingmore
}
When there's somethingelse to do if SV *is* found, try this:
#!/bin/bash
if [[ $VAR != *SV* ]] ; then
dosomething
else
dosomethingelse
fi
I do this to update PATH:
[[ :$PATH: != *:/some/dir:* ]] && export PATH=/some/dir:$PATH
Note that this will test for SV as a prefix, infix, or suffix
--
David N. Lombard
More information about the ubuntu-users
mailing list