Date: Fri, 12 Aug 2005 13:38:34 -0700 From: Benson Wong <tummytech@gmail.com> To: Ian Smith <smithi@nimnet.asn.au> Cc: Xu Qiang <Qiang.Xu@fujixerox.com>, freebsd-questions@freebsd.org Subject: Re: Help on bash script? Message-ID: <860807bf050812133839dd8f46@mail.gmail.com> In-Reply-To: <Pine.BSF.3.96.1050813044601.8009A-100000@gaia.nimnet.asn.au> References: <20050812013604.8C12D16A422@hub.freebsd.org> <Pine.BSF.3.96.1050813044601.8009A-100000@gaia.nimnet.asn.au>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <smithi@nimnet.asn.au> wrote:
> On Fri 12 Aug 2005 09:33:54 +0800 Xu Qiang <Qiang.Xu@fujixerox.com> 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 <tempfile
> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?860807bf050812133839dd8f46>
