On Apr 9, 2011, at 6:31 AM, Dino Vliet <[email protected]> wrote:
> Hi folks,
> I'm having trouble with a little shell script. Can somebody explain me why I
> get 3 times "expr: syntax error" in my console after I run this little
> script?
>
> #! /usr/local/bin/bash
> # testscript
>
> var1="trees.J48" #other value will be rules.Jrip, rules.DecisionTable
> len=${#var1}
> ind=`expr index $var1 s`
> pos=`expr $len - $ind`
> out=`expr substr $var1 $ind $pos`
>
> I would expect (and want the following to happen):
>
> $ind should contain 6
> $pos should contain 3
> $out should contain J48 (other values will have to be Jrip,DecisionTable)
>
> Can anyone help me with this?
This would be a /bin/sh compatible (read: portable) way to accomplish the above:
#!/bin/sh
# testscript
var1="trees.J48" #other value will be rules.Jrip, rules.DecisionTable
len=${#var1}
ind=`echo "$var1" | awk '{print index($0,"s")+1}'`
pos=$(( $len - $ind ))
out=`echo "$var1" | awk -vind="$ind" -vpos="$pos" '{print
substr($0,ind+1,pos)}'`
Though, there are certainly easier ways to get at what it is that I assume your
after:
#!/bin/sh
# testscript
var1="trees.J48" #other value will be rules.Jrip, rules.DecisionTable
out="${var1##*.}"
--
Devin
>
> Thanks
>
> _______________________________________________
> [email protected] mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "[email protected]"
_____________
The information contained in this message is proprietary and/or confidential.
If you are not the intended recipient, please: (i) delete the message and all
copies; (ii) do not disclose, distribute or use the message in any manner; and
(iii) notify the sender immediately. In addition, please be aware that any
message addressed to our domain is subject to archiving and review by persons
other than the intended recipient. Thank you.
_____________
_______________________________________________
[email protected] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[email protected]"