Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Oct 2005 15:24:17 -0700
From:      David Kirchner <dpk@dpk.net>
To:        Drew Tomlinson <drew@mykitchentable.net>
Cc:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Help Understanding While Loop
Message-ID:  <35c231bf0510141524u133f2d1bkeb46d60e112ee413@mail.gmail.com>
In-Reply-To: <435027A3.8000908@mykitchentable.net>
References:  <435027A3.8000908@mykitchentable.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On 10/14/05, Drew Tomlinson <drew@mykitchentable.net> 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?35c231bf0510141524u133f2d1bkeb46d60e112ee413>