From owner-freebsd-questions@FreeBSD.ORG Sat Apr 9 15:57:31 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E7315106566C for ; Sat, 9 Apr 2011 15:57:31 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) by mx1.freebsd.org (Postfix) with ESMTP id AA4CC8FC0A for ; Sat, 9 Apr 2011 15:57:31 +0000 (UTC) Received: from r55.edvax.de (port-92-195-63-56.dynamic.qsc.de [92.195.63.56]) by mx01.qsc.de (Postfix) with ESMTP id 309243CA8D; Sat, 9 Apr 2011 17:57:30 +0200 (CEST) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id p39FvTeF013735; Sat, 9 Apr 2011 17:57:29 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Sat, 9 Apr 2011 17:57:29 +0200 From: Polytropon To: Dino Vliet Message-Id: <20110409175729.357bbb91.freebsd@edvax.de> In-Reply-To: <824253.34690.qm@web113616.mail.gq1.yahoo.com> References: <824253.34690.qm@web113616.mail.gq1.yahoo.com> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: shell programming question: help with expr command X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Apr 2011 15:57:32 -0000 On Sat, 9 Apr 2011 06:31:28 -0700 (PDT), Dino Vliet = wrote: > Hi folks, > I'm having trouble with a little shell script. Can somebody > explain me why I get 3 times "expr: syntax=A0 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? The explaination is quite simple: expr doesn't know "index" or "substr"; see "man expr" for details. A polite sidenote: Unless you have a good reason to code in bash-specific manner, do NOT #!/usr/local/bin/bash, as this is NOT portable (if this is one of your goals); use the standard #!/bin/sh instead. Depending on what you have in mind, maybe mentioning the strengths of perl, sed and awk is worth being mentioned. :-) --=20 Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...