delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/05/16/19:39:26

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
To: cygwin AT cygwin DOT com
X-Injected-Via-Gmane: http://gmane.org/
Path: not-for-mail
From: Andrew DeFaria <ADeFaria AT Salira DOT com>
Newsgroups: gmane.os.cygwin
Subject: bash question
Date: Thu, 16 May 2002 16:38:53 -0700
Lines: 47
Message-ID: <3CE4430D.5050503@Salira.com>
Reply-To: cygwin AT cygwin DOT com
NNTP-Posting-Host: 206.184.204.2
Mime-Version: 1.0
X-Trace: main.gmane.org 1021592304 6020 206.184.204.2 (16 May 2002 23:38:24 GMT)
X-Complaints-To: usenet AT main DOT gmane DOT org
NNTP-Posting-Date: Thu, 16 May 2002 23:38:24 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0rc2) Gecko/20020510
X-Accept-Language: en-us, en

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/

- Raw text -


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