From owner-freebsd-questions@FreeBSD.ORG Fri Oct 14 22:24:18 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C851616A41F for ; Fri, 14 Oct 2005 22:24:18 +0000 (GMT) (envelope-from dpkirchner@gmail.com) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.203]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6346043D45 for ; Fri, 14 Oct 2005 22:24:18 +0000 (GMT) (envelope-from dpkirchner@gmail.com) Received: by xproxy.gmail.com with SMTP id t13so476456wxc for ; Fri, 14 Oct 2005 15:24:17 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=qaHfwL7j0OmNiRKFPML+Cd0xIJOy+Z/z7/Nhp4AQF/sitphXh6k3IsKyz5bcXWwoAWKvxmeuT6JFPr5RLWm/OFPpep0fLK3XrfBJgQldDga89NC2D0c+DHmBo5Xu+XeU1IUogRK4Y68FJbCxt2TIuZNfDzUfdGMEWSjt9lgFKAg= Received: by 10.70.103.13 with SMTP id a13mr1472521wxc; Fri, 14 Oct 2005 15:24:17 -0700 (PDT) Received: by 10.70.104.20 with HTTP; Fri, 14 Oct 2005 15:24:17 -0700 (PDT) Message-ID: <35c231bf0510141524u133f2d1bkeb46d60e112ee413@mail.gmail.com> Date: Fri, 14 Oct 2005 15:24:17 -0700 From: David Kirchner Sender: dpkirchner@gmail.com To: Drew Tomlinson In-Reply-To: <435027A3.8000908@mykitchentable.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <435027A3.8000908@mykitchentable.net> Cc: FreeBSD Questions Subject: Re: Help Understanding While Loop X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Oct 2005 22:24:18 -0000 On 10/14/05, Drew Tomlinson wrote: > OK, I've been working on an sh script and I'm almost there. In the > script, I created a 'while read' loop that is doing what I want. Now I > want to keep track of how many times the loop executes. Thus I included > this line between the 'while read' and 'done' statements: > > count =3D $(( count + 1 )) > > I've tested this by adding an 'echo $count' statement in the loop and it > increments by one each time the loop runs. However when I attempt to > call $count in an 'echo' statement after the 'done', the variable is > null. Thus I assume that $count is only local to the loop and I have to > export it to make it available outside the loop? What must I do? Oh yeah, that's another side effect of using the while read method. Because it's "| while read" it's starting a subshell, so any variables are only going to exist there. You'd need to have some sort of 'echo' within the while read, and then | wc -l at the end of the while loop, or something along those lines. The IFS method someone else mentioned, in regards to 'for' loops, would probably be better all around. So you'd want: OLDIFS=3D$IFS # Note this is a single quote, return, single quote, no spaces IFS=3D' ' for i in `find etc` do done IFS=3D$OLDIFS