From owner-freebsd-stable Sun Aug 11 3:20:55 2002 Delivered-To: freebsd-stable@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 466D037B400 for ; Sun, 11 Aug 2002 03:20:51 -0700 (PDT) Received: from maila.telia.com (maila.telia.com [194.22.194.231]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3AE8D43E65 for ; Sun, 11 Aug 2002 03:20:40 -0700 (PDT) (envelope-from erikt@midgard.homeip.net) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by maila.telia.com (8.12.5/8.12.5) with ESMTP id g7BAKMWE008784 for ; Sun, 11 Aug 2002 12:20:22 +0200 (CEST) X-Original-Recipient: Received: from falcon.midgard.homeip.net (h62n2fls20o913.telia.com [212.181.163.62]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id MAA04318 for ; Sun, 11 Aug 2002 12:20:21 +0200 (CEST) Received: (qmail 41022 invoked by uid 1001); 11 Aug 2002 10:20:12 -0000 Date: Sun, 11 Aug 2002 12:20:12 +0200 From: Erik Trulsson To: Chris Knight Cc: freebsd-stable@freebsd.org Subject: Re: variable scope issue in -stable? Message-ID: <20020811102010.GA40612@falcon.midgard.homeip.net> Mail-Followup-To: Chris Knight , freebsd-stable@freebsd.org References: <014d01c2411a$a2bee5d0$020aa8c0@aims.private> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <014d01c2411a$a2bee5d0$020aa8c0@aims.private> User-Agent: Mutt/1.5.1i Sender: owner-freebsd-stable@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Aug 11, 2002 at 07:36:56PM +1000, Chris Knight wrote: > Howdy, > > I've got the following problem in FreeBSD-stable, and also 4.6, 4.5 and 4.2. > The following code is not behaving as expected: > ----- > #!/bin/sh > match=0 > anycmd | while read line > do > case "$line" in > "command") > match=1 > echo "match=$match" > ;; > *) > ;; > esac > done > echo "match=$match" > ----- > The output of the above results in: > match=1 > match=0 > > I'd expect match to be a global definition, but match loses its value > outside of the while block. > It appears that read stuffs things up - if I change the while expression to > something other than read, then the variable scope behaves correctly. > Or am I missing something? The following paragraph from the sh(1) manpage seems relevant: Note that unlike some other shells, sh executes each process in the pipeline as a child of the sh process. Shell builtin commands are the exception to this rule. They are executed in the current shell, although they do not affect its environment when used in pipelines. So it is not the read, but the pipe that makes things behave differently from what you expect. -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message