From owner-freebsd-questions@FreeBSD.ORG Fri Aug 12 20:38:35 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 7088316A41F for ; Fri, 12 Aug 2005 20:38:35 +0000 (GMT) (envelope-from tummytech@gmail.com) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.206]) by mx1.FreeBSD.org (Postfix) with ESMTP id CDC1D43D53 for ; Fri, 12 Aug 2005 20:38:34 +0000 (GMT) (envelope-from tummytech@gmail.com) Received: by rproxy.gmail.com with SMTP id i8so511867rne for ; Fri, 12 Aug 2005 13:38:34 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=jeHuo5cRwVUN9NJZ5GnPbI1ch9jPU++Q3u3FL463uqf1nZYe5Wt3TMCVXeIjii70uqNJyT4ers93OT5L49Wl/LDqht08m+B7ZVfGN0p6VxEEYzl+9Qdu9o18FXowGlwkPPol/fewpinuTsnPCZiqSiMZklEw+uXL8JLmK9smgBI= Received: by 10.39.3.36 with SMTP id f36mr1133035rni; Fri, 12 Aug 2005 13:38:34 -0700 (PDT) Received: by 10.38.88.61 with HTTP; Fri, 12 Aug 2005 13:38:34 -0700 (PDT) Message-ID: <860807bf050812133839dd8f46@mail.gmail.com> Date: Fri, 12 Aug 2005 13:38:34 -0700 From: Benson Wong To: Ian Smith In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <20050812013604.8C12D16A422@hub.freebsd.org> Cc: Xu Qiang , freebsd-questions@freebsd.org Subject: Re: 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 20:38:35 -0000 I prefer:=20 for COREFILE in `find / -type f -name core -print` do=20 ...=20 done Wouldn't that accomplish the same thing?=20 On 8/12/05, Ian Smith wrote: > On Fri 12 Aug 2005 09:33:54 +0800 Xu Qiang wrote= : >=20 > > find / -type f -name core -print | while read COREFILE ; = do > > NCOREFILES=3D$[ $NCOREFILES + 1 ] # a bit strange= - xq > > echo $NCOREFILES # xq > > > > NEWNAME=3D"${HOSTNAME}esscore${NCOREFILES}_${TIME= STAMP}" > > # 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 >=20 > 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: >=20 > find / -type f -name "*.core" -print >tempfile > while read COREFILE; do > [.. stuff ..] > NCOREFILES=3D$(($NCOREFILES + 1)) > done echo $NCOREFILES >=20 > I use sh, not bash, but suspect that this should work in bash too. >=20 > cheers, Ian >=20 > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.o= rg" >=20 --=20 blog: http://www.mostlygeek.com