[plug] strncmp in shell
Russell Steicke
r.steicke at bom.gov.au
Mon Jan 19 14:18:47 WST 2004
On Mon, Jan 19, 2004 at 01:51:37PM +0800, Cameron Patrick wrote:
> Hi all,
>
> Does anyone here know what the equivalent of C's strncmp in shell
> scripting is? (Specifically I'm using bash, but the machines it'll be
> running on will have zsh too and I'm willing to resort to that if
> there's no easy way to do it with bash.)
>
> What I want to do is say something like
> if [ "$FOO[0-6]" = "wurble" ]; then (do stuff) fi
> equivalent to C's
> if (!strncmp(foo, "wurble", 6)) { stuff(); }
> or Python's:
> if foo[0:6] == 'wurble': stuff()
Something like this?
$ FOO=one
$ echo ${FOO/one/}
$ echo ${FOO/onex/}
one
$ echo ${FOO/on/}
e
$ if [ -z "${FOO/one/}" ] ; then echo yes ; fi
yes
$ if [ -z "${FOO/on/}" ] ; then echo yes ; fi
$ if [ -z "${FOO/onee/}" ] ; then echo yes ; fi
$ if [ -z "${FOO/one/}" ] ; then echo yes ; fi
yes
$
I've never used these things in anger, but know they exist. You might
also try the ${FOO#bar} construct.
--
Russell Steicke
-- Fortune says:
But what can you do with it? -- ubiquitous cry from Linux-user partner.
(Submitted by Andy Pearce, ajp at hpopd.pwd.hp.com)
More information about the plug
mailing list