From owner-freebsd-questions@FreeBSD.ORG Fri Aug 12 19:18:26 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id DC38316A41F for ; Fri, 12 Aug 2005 19:18:26 +0000 (GMT) (envelope-from smithi@nimnet.asn.au) Received: from gaia.nimnet.asn.au (nimbin.lnk.telstra.net [139.130.45.143]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6FB9643D45 for ; Fri, 12 Aug 2005 19:18:24 +0000 (GMT) (envelope-from smithi@nimnet.asn.au) Received: from localhost (smithi@localhost) by gaia.nimnet.asn.au (8.8.8/8.8.8R1.4) with SMTP id FAA08675; Sat, 13 Aug 2005 05:18:21 +1000 (EST) (envelope-from smithi@nimnet.asn.au) Date: Sat, 13 Aug 2005 05:18:21 +1000 (EST) From: Ian Smith To: freebsd-questions@freebsd.org In-Reply-To: <20050812013604.8C12D16A422@hub.freebsd.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Cc: Xu Qiang Subject: Help on bash script? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Aug 2005 19:18:27 -0000 On Fri 12 Aug 2005 09:33:54 +0800 Xu Qiang wrote: > find / -type f -name core -print | while read COREFILE ; do > NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq > echo $NCOREFILES # xq > > NEWNAME="${HOSTNAME}esscore${NCOREFILES}_${TIMESTAMP}" > # record mapping so people can go back and figure out > # where they came from > echo -e $NEWNAME " was " `ls -l $COREFILE` >> $SAVE_DIR/$CORELOG > mv $COREFILE $SAVE_DIR/$NEWNAME > > echo "There are $NCOREFILES core files." # xq > done > > fi > > # What confused me most is the value $NCOREFILES outside > # the do-while loop (but still in this function) reverted > # back to its initial value, which seems contradictory to > # our concept of local variables. - xq > #echo $NCOREFILES It's been pointed out that the find piped to while runs in a subshell, and that changes to variables within aren't seen by its parent, but one way around this is to avoid using a pipe, thus a subshell, for instance: find / -type f -name "*.core" -print >tempfile while read COREFILE; do [.. stuff ..] NCOREFILES=$(($NCOREFILES + 1)) done