From owner-freebsd-current Tue Feb 18 8:18:39 2003 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7FE8A37B401 for ; Tue, 18 Feb 2003 08:18:38 -0800 (PST) Received: from harris.cwru.edu (harris.CNS.CWRU.Edu [129.22.104.63]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7DA4C43F75 for ; Tue, 18 Feb 2003 08:18:37 -0800 (PST) (envelope-from chet@nike.ins.cwru.edu) Registered-mail-reply-requested-by: chet@po.cwru.edu Received: from nike.ins.cwru.edu (nike.INS.CWRU.Edu [129.22.8.219]) by smtp-a.cwru.edu (iPlanet Messaging Server 5.2 Patch 1 (built Aug 19 2002)) with SMTP id <0HAI00CKQIN03Z@smtp-a.cwru.edu> for current@freebsd.org; Tue, 18 Feb 2003 11:18:36 -0500 (EST) Date: Tue, 18 Feb 2003 11:14:48 -0500 From: Chet Ramey Subject: Re: Shell programming 101: Is this an expr(1) bug ? In-reply-to: <"Message from phk"@phk.freebsd.dk> To: phk@phk.freebsd.dk Cc: current@freebsd.org Reply-To: chet@cwru.edu Message-id: <030218161448.AA93484.SM@nike.ins.cwru.edu> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT References: <57374.1045568362@critter.freebsd.dk> Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG > Running: > > #!/bin/sh > set -ex > > for p in ad2 ad0 ad1 > do > a0=`expr $p : '^ad\([0-9]\)$'` > done > > I get: > > syv# sh _ > + expr ad2 : ^ad\([0-9]\)$ > + a0=2 > + expr ad0 : ^ad\([0-9]\)$ > + a0=0 > syv# echo $? > 1 The `set -e' says to exit the shell if a simple command fails. POSIX.2 says that the exit status of an assignment statement without an accompanying command is either 0 or the exit status of the last command substitution performed while expanding the rhs. `expr' returns 1 when the result is `0'. Thus the assignment statement fails and the shell exits. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ( ``Discere est Dolere'' -- chet ) Chet Ramey, ITS, CWRU chet@po.CWRU.Edu http://cnswww.cns.cwru.edu/~chet/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message