Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
From: "Bernard A Badger" <bab@vx.com>
To: "Mark Blackburn" <marklist@fangorn.ca>, <cygwin@cygwin.com>
Subject: RE: bash question
Date: Fri, 17 May 2002 10:01:26 -0400
Message-ID: <INEKLKBFCDBPKMKAJLMDMEOLCCAA.bab@vx.com>
MIME-Version: 1.0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
In-Reply-To: <Pine.LNX.3.96.1020516235124.18127C-100000@rivendell.fangorn.ca>
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700



> -----Original Message-----
> From: cygwin-owner@cygwin.com [mailto:cygwin-owner@cygwin.com]On Behalf
> Of Mark Blackburn
> Sent: Thursday, May 16, 2002 11:55 PM
> To: cygwin@cygwin.com
> Subject: Re: bash question
> 
> 
> You asked this in the wrong place btw, (I think its a bash specific
> questing) but here goes anyways:
> #!/bin/bash
> 
> i=0
> for x in 1 2 3; do
>   let i=i+1
>   echo "item $x"
> done
> 
> echo "Processed $i items"
> 
> cat > /tmp/file <<END
> item 1
> item 2
> item 3
> END
> 
> cat /tmp/file | { export i=0; while read item; do \
>   let i=i+1 ; \
>   echo "Read $item $i" ; \
> done }
> 
> echo "Processed $i items"
> 
> rm -f /tmp/file
> 
> output is:
> item 1
> item 2
> item 3
> Processed 3 items
> Read item 1 1
> Read item 2 2
> Read item 3 3
> Processed 3 items
> 
Sorry, wrong answer.  You're just printing out the first count again.
The second count is lost, but happens to be 3 also.  Add couple of lines
to the file to see:
$ ./tscript.sh
item 1
item 2
item 3
Processed 3 items
Read item 1 1
Read item 2 2
Read item 3 3
Read item 4 4
Read item 5 5
Processed 3 items

The following version works:
#!/bin/bash

i=0
for x in 1 2 3; do
  let i=i+1
  echo "item $x"
done

echo "Processed $i items"

i=0
cat > /tmp/file <<END
item 1
item 2
item 3
item 4
item 5
END

while read item; do
  let i=i+1 ;
  echo "Read $item $i" ;
done < /tmp/file  #NOTE this placement of redirection
####################################### now run it
$ ./tscript.sh
item 1
item 2
item 3
Processed 3 items
Read item 1 1
Read item 2 2
Read item 3 3
Read item 4 4
Read item 5 5
Processed 5 items
> 
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting:         http://cygwin.com/bugs.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

