Date: Thu, 5 Dec 2019 13:35:21 +0000 From: RW <rwmaillists@googlemail.com> To: freebsd-questions@freebsd.org Subject: Re: Counter in sh inside loop, value "encapsulation" Message-ID: <20191205133521.28cb5ac4@gumby.homeunix.com> In-Reply-To: <slrnqug5mi.srf.naddy@lorvorc.mips.inka.de> References: <20191204181300.8dd0e03c.freebsd@edvax.de> <slrnqug5mi.srf.naddy@lorvorc.mips.inka.de>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 4 Dec 2019 20:28:02 -0000 (UTC) Christian Weisgerber wrote: > On 2019-12-04, Polytropon <freebsd@edvax.de> wrote: > > > #!/bin/sh > > COUNT=0 > > grep "^https" ${INFILE} | while read URL; do > > COUNT=`expr ${COUNT} + 1` > > done > > echo "URLs processed: ${COUNT}" # <--- (!) THIS IS ZERO! > > Each command of a pipeline is executed in a subshell. > (Some shells, notably AT&T ksh, behave differently.) > > This is the single most asked shell scripting question. IIRC when this sort of thing came once before someone posted a way of processing the output of a pipeline and having multiple variables directly accessible to the main script. It didn't involve creating files or sockets, or storing the output of the pipeline in a single variable. There was some trick that avoided last stage being done in sub-shell. Unfortunately, I forgot to make a note of it and I haven't been able to find the thread again. Anyone know what it might be?
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20191205133521.28cb5ac4>