From owner-freebsd-questions@freebsd.org Thu Dec 5 04:11:53 2019 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B066F1B9983 for ; Thu, 5 Dec 2019 04:11:53 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47T2NR7170z3FBj for ; Thu, 5 Dec 2019 04:11:51 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([178.12.46.74]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPA (Nemesis) id 1MpUIW-1hxwhr0V9I-00pwSh; Thu, 05 Dec 2019 05:11:47 +0100 Date: Thu, 5 Dec 2019 05:11:45 +0100 From: Polytropon To: Christian Weisgerber Cc: freebsd-questions@freebsd.org Subject: Re: Counter in sh inside loop, value "encapsulation" Message-Id: <20191205051145.78f9a805.freebsd@edvax.de> In-Reply-To: References: <20191204181300.8dd0e03c.freebsd@edvax.de> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:7OthySKKTaPWljgwIMLnww/OJ+ykR8yXmN+Ouwj9C7jno4hzp2F alPgt3mMsDr4IjiFazU39d1LTR1svnd+31UmN9iLXNlzZKn45ODL59+xEcAJ5DT18lZ5hCK hU3SWUKLbZd73f9OAp0Uo48PiAagZijZJglGGjZqVHjR3QwfSqlUCIAmZT/I+PfUN8R4JV8 bhajwKfigTDonQdGgSTFQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:4DflpZj/sDM=:ugE1nysrbpUWMKj4VG34Zv GbpycvhMNR0atvvargNus3MK5qJ+bb/s6ny60Nu9zcGHJUPnP+uxDY2lkhlu8k37b1TaUwVhf xulrTBSDd7FbFJ03pJUpiQztETagdEsQlwMJbsKHjymctkxbH/Sbw3CXi9/ZKl7Wo6CqtnJrE EAea3XAoDJQiZYhltri0AV4vyWNq7A3oRJBWYF0EQlgiJmQ/nql2+FHMlrGaHi2uwoWmqRfKP qn3WzSpQeFVbOuzw1VmjsAsgyUrW4BhNGMH5lESQZH4sg9IZu/q93kVhidFCIUMgp0VvT7JKZ Mkef6etqjW8P+tdzeiZRLYkfVTY679qnnewr9vQ4VDjDBOD+78zV9QQCUqFr7LX0NBgzMEQ0t lhmTddwicXDPVyXr0mfsWWrFKOdmC0dlqZKLflwiqgyNOEnvzc81mLafGkAkqbN8b8pa1iWKU LrakERy/vF3sTZHYp+v4L/KQet0iwIXi1yZMvEStQJGvm9AWqcPVi182/RX64VQ+NIrOUE85L vOHalG11+cb+GhPRmTHJr+4eg7dAZ/90J9JbikSbT1DtJxDXXaFTjgCXvpZjTZbaAYpVGMDaL y8R85aNufmZed4fWu+pJewUbbntiX4VShcS9h+azWIRrMTGpNcW72gCh5lle2sO8cRQndwwNh 9wO/siIFQ/7TEfiY1UuVo7gtZqnTpPJXfaIWDd/9gXLMsESGho6A0PV/m1lQbI4i8f4RcFEOC 3RUkGxt4chQWePfTEUu9sYDNCkxFRQZpYjnTpaK724WvuIQHgvd8+8eInLctWAxXehVR4/Gyx mX/VH0wk5j4T42Vulo1OT3o/sTWLlwoMGTfEJw+AE+H0UJOz2FmyIg0JYEUvhf2Gr1puC7o76 xuRx/bBlGMC7Ln4aX8ng== X-Rspamd-Queue-Id: 47T2NR7170z3FBj X-Spamd-Bar: ++++ Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of freebsd@edvax.de has no SPF policy when checking 212.227.126.187) smtp.mailfrom=freebsd@edvax.de X-Spamd-Result: default: False [4.40 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[freebsd@edvax.de]; TO_DN_SOME(0.00)[]; MV_CASE(0.50)[]; HAS_ORG_HEADER(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; RECEIVED_SPAMHAUS_PBL(0.00)[74.46.12.178.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.11]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; ASN(0.00)[asn:8560, ipnet:212.227.0.0/16, country:DE]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_GOOD(-0.10)[text/plain]; RCVD_TLS_LAST(0.00)[]; DMARC_NA(0.00)[edvax.de]; AUTH_NA(1.00)[]; NEURAL_SPAM_MEDIUM(0.84)[0.843,0]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_SPAM_LONG(0.85)[0.852,0]; MID_CONTAINS_FROM(1.00)[]; RCVD_IN_DNSWL_NONE(0.00)[187.126.227.212.list.dnswl.org : 127.0.5.0]; R_SPF_NA(0.00)[]; RWL_MAILSPIKE_POSSIBLE(0.00)[187.126.227.212.rep.mailspike.net : 127.0.0.17]; RCVD_COUNT_TWO(0.00)[2]; IP_SCORE(0.30)[ip: (0.38), ipnet: 212.227.0.0/16(-1.14), asn: 8560(2.29), country: DE(-0.01)] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Dec 2019 04:11:53 -0000 On Wed, 4 Dec 2019 20:28:02 -0000 (UTC), Christian Weisgerber wrote: > On 2019-12-04, Polytropon 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. Okay, so exactly what I assumed - and what is probably buried as basic knowledge in the backyard of my rotting brain. ;-) Due to portability reasons, I can only use sh (and not bash). There are other tools available like sed, awk, cut, paste, tee, all the nice little standard tools you can expect everywhere. I would now probably try to do something like this: grep ... | while ... do ... echo "URLs processed: ${COUNT}" done | tee result.txt COUNT=`awk '' result.txt` The awk code will match the last of the "URLs processed:" messages and return the value after the ":". That of course is hardly an elegant solution, but will surely work. It will probably be some /^.../ and END { printf(...) } magic in there. :-) On Wed, 4 Dec 2019 16:39:36 -0300, Mario Lobo wrote: > Could you try that? > > #!/usr/local/bin/bash ^^^^^^^^^^^^^^^^^^^ Sadly not. Due to portability reasons, and availability of only the most fundamental UNIX tools on different platforms where I don't have any kind of control to install bash, this option is not possible. I also thought about getting rid of the pipe as the main source of the "encapsulation problem", which I would maybe try in a later revision of the script. However this probably won't happen due to "never touch a running system", "works on my machine", and "we don't have time for that." ;-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...