Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Apr 2011 09:47:16 -0700
From:      Devin Teske <dteske@vicor.com>
To:        Dino Vliet <dino_vliet@yahoo.com>
Cc:        "freebsd-questions@freebsd.org" <freebsd-questions@freebsd.org>
Subject:   Re: shell programming question: help with expr command
Message-ID:  <90087016-3918-4F74-A768-67023E3C2B85@vicor.com>
In-Reply-To: <824253.34690.qm@web113616.mail.gq1.yahoo.com>
References:  <824253.34690.qm@web113616.mail.gq1.yahoo.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
On Apr 9, 2011, at 6:31 AM, Dino Vliet <dino_vliet@yahoo.com> wrote:

> Hi folks,
> I'm having trouble with a little shell script. Can somebody explain me wh=
y I get 3 times "expr: syntax  error" in my console after I run this little=
 script?
>=20
> #! /usr/local/bin/bash
> # testscript=20
>=20
> var1=3D"trees.J48" #other value will be rules.Jrip, rules.DecisionTable
> len=3D${#var1}
> ind=3D`expr index $var1 s`
> pos=3D`expr $len - $ind`
> out=3D`expr substr $var1 $ind $pos`=20
>=20
> I would expect (and want the following to happen):
>=20
> $ind should contain 6
> $pos should contain 3
> $out should contain J48 (other values will have to be Jrip,DecisionTable)
>=20
> Can anyone help me with this?

This would be a /bin/sh compatible (read: portable) way to accomplish the a=
bove:

#!/bin/sh
# testscript=20
var1=3D"trees.J48" #other value will be rules.Jrip, rules.DecisionTable
len=3D${#var1}
ind=3D`echo "$var1" | awk '{print index($0,"s")+1}'`
pos=3D$(( $len - $ind ))
out=3D`echo "$var1" | awk -vind=3D"$ind" -vpos=3D"$pos" '{print substr($0,i=
nd+1,pos)}'`

Though, there are certainly easier ways to get at what it is that I assume =
your after:

#!/bin/sh
# testscript
var1=3D"trees.J48" #other value will be rules.Jrip, rules.DecisionTable
out=3D"${var1##*.}"

--=20
Devin

>=20
> Thanks
>=20
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.o=
rg"

_____________

The information contained in this message is proprietary and/or confidentia=
l. If you are not the intended recipient, please: (i) delete the message an=
d all copies; (ii) do not disclose, distribute or use the message in any ma=
nner; and (iii) notify the sender immediately. In addition, please be aware=
 that any message addressed to our domain is subject to archiving and revie=
w by persons other than the intended recipient. Thank you.
_____________



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?90087016-3918-4F74-A768-67023E3C2B85>