test for a pattern in sh?

Nils Kassube kassube at gmx.net
Sun Oct 11 20:12:02 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.

How about this?

if [ "$VAR" = "${VAR#*SV}" ];then do_something;fi

The term ${VAR#*SV} is the same as $VAR if there is no SV in it. 
Otherwise ${VAR#*SV} removes the first part of the string including the 
SV which makes both strings different, no matter where the SV is.


Nils





More information about the ubuntu-users mailing list