delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/05/17/10:37:27

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: "Bernard A Badger" <bab AT vx DOT com>
To: "Mark Blackburn" <marklist AT fangorn DOT ca>, <cygwin AT cygwin DOT com>
Subject: RE: bash question
Date: Fri, 17 May 2002 10:01:26 -0400
Message-ID: <INEKLKBFCDBPKMKAJLMDMEOLCCAA.bab@vx.com>
MIME-Version: 1.0
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 AT cygwin DOT com [mailto:cygwin-owner AT cygwin DOT com]On Behalf
> Of Mark Blackburn
> Sent: Thursday, May 16, 2002 11:55 PM
> To: cygwin AT cygwin DOT 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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019