I may have asked this before but I'd really like to understand this and 
get a fix for it. Given the following script:

#!/bin/bash

declare -i 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

declare -i i=0

cat /tmp/file | while read item; do
  let i=i+1
  echo "Read $item"
done

echo "Processed $i items"

rm -f /tmp/file

I get the following output:

$ test.sh
item 1
item 2
item 3
Processed 3 items
Read item 1
Read item 2
Read item 3
Processed 0 items

The quesiton is why is the integer "i" reporting 0 items processed for 
the second loop? I know that it has to do with the fact that a pipe is 
involved. However, how can I code this so that "i" survives?




--
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/

Reply via email to