> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf
> Of Mark Blackburn
> Sent: Thursday, May 16, 2002 11:55 PM
> To: [EMAIL PROTECTED]
> 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/