From owner-freebsd-questions@freebsd.org Wed Dec 4 17:13:10 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 BC61B1A8C2C for ; Wed, 4 Dec 2019 17:13:10 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.130]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass Class 2 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47SlmP3yDPz45ML for ; Wed, 4 Dec 2019 17:13:09 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([94.222.22.63]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPA (Nemesis) id 1M72Xn-1ihEtk3xst-008b8p for ; Wed, 04 Dec 2019 18:13:04 +0100 Date: Wed, 4 Dec 2019 18:13:00 +0100 From: Polytropon To: FreeBSD Questions Subject: Counter in sh inside loop, value "encapsulation" Message-Id: <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:yo7HOxWF/ivwIX3DcPN/RcM5aP+pL24ANspYkT+SPD70K2WHohz xeznmQhkccc4wbvSHjWEV8r1JbUBbpvUoW0knSSNMLrBnP6htFhqQcSaqTT/ZKk+E3P+H84 Jgc0jbsO5Qnd5ZnPn3lm0SD7qqODsoX6RPH2r9ephADhA0Vtiuw7HSBRtunllLFX+Kfa4Xi PtVtVvY9wIGKM2e+Lr5xQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:iJmLE6jebbk=:w0YanrVC4QwNvUQkhGKB50 zEuG/GUEdfy2zE06ONQpOhTrqr7mrpTbB9h9GfRpmurd6WC39qczKcKsnhA4bZmsY/R5Y8lgK rnqNjMvuF9pq9cv5jc1QVInK+DOqrTIn+m5umoSi8je2o3jfDb4h9JOzRO903izVKfLevlMCP D3A5u6VhOIMHu85YI/4cCAbuDZPwP613NYRbf/zYTWCZSWjkz5JsZZyI+AFve/piUnRDdecWw BkPxzsMmdp3mh3J23raUnSp24UJ0hhPf72nvy/iM6LYU5cFXpk7Lo1EmdsH0sHosbtNXhYql4 H0KdrkEz5sIO92YqgSZSfSuiQ4nc27ckvjrbxpDpMaf1PV4bsPmtH83WMaKhc3YdmhFAwxhxv Vwwo+zDLCMhTPND8apd7Iu9BUn1HTXok8pcTX/9Sc2GdIFpsxYV7a5PxoCeyUzvzkshxeazZ0 FFgHCN4/7fJZfSzffayq1gggS4V1aXNKSzFwpdXHjarfiGuoAT+RN7+li4E0EzeDnJoZZBvso zWakZafhswcmzagf4glRlal+bC8i+1u7ByjHn3xnCh2VRHhnbuT0v/5n2fZZi7PHShHAinawJ OG5ExyDNP0JHkeylTBYayIQk0d3oU2dTbRxfaplrg26tNwCZ7nWzxPDsn4YGUvRAS1GvYDy+S P0EDYxSqx6C2puHcAa9YgbNE0NyV4zO+n+nMtOQ42UMok312ggIjsWfvv+aDD+i8YuRAAk1hL uFgNigZIzlQ4LWuQ29EBBN1HK+e4l2lQ7pHAUazsWHfD0nbGjvbSFMiMmIG+4AJURvfAdvEzI LIOJanQFFyqQ16NRpBdo4DcF7zMuntGfn/JYbiHW+mdZiA8dMOxOa3AFvMdy3VOP57l7LyfiI WGvWPBXwXTpa2tiQheXA== X-Rspamd-Queue-Id: 47SlmP3yDPz45ML 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.130) smtp.mailfrom=freebsd@edvax.de X-Spamd-Result: default: False [5.05 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[freebsd@edvax.de]; MV_CASE(0.50)[]; HAS_ORG_HEADER(0.00)[]; TO_DN_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(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:+]; RECEIVED_SPAMHAUS_PBL(0.00)[63.22.222.94.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.11]; ARC_NA(0.00)[]; REPLYTO_EQ_FROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; DMARC_NA(0.00)[edvax.de]; AUTH_NA(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_SPAM_MEDIUM(0.99)[0.989,0]; IP_SCORE(0.67)[ip: (2.23), ipnet: 212.227.0.0/16(-1.15), asn: 8560(2.29), country: DE(-0.01)]; NEURAL_SPAM_LONG(0.99)[0.989,0]; MID_CONTAINS_FROM(1.00)[]; RCVD_IN_DNSWL_NONE(0.00)[130.126.227.212.list.dnswl.org : 127.0.5.0]; R_SPF_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2] 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: Wed, 04 Dec 2019 17:13:10 -0000 In a sh script, I have a counter inside an interator. The iterator is fed by piping with some greo output. It looks like this: #!/bin/sh # ... # ... lots of boring stuff omitted ... # ... echo "Start: ${TIMESTAMP}" echo "Input: ${INFILE} (${TOTAL} entries, ${SKIPPED} skipped, ${REMAINING} remaining)" COUNT=0 grep "^https" ${INFILE} | while read URL; do COUNT=`expr ${COUNT} + 1` echo echo " ${COUNT} / ${REMAINING}" echo "---> ${URL}" # ... # ... processing per URL, also boring ... # ... done echo echo "URLs processed: ${COUNT}" # <--- (!) THIS IS ZERO! echo -n "Time: ${TIMESTAMP} - " date "+%Y-%m-%d %H%:%M:%S" echo "" exit 0 So while the loop runs, the counter is increased for each URL which is processed. After leaving the loop, $COUNT is zero, for example: Input: bla.txt (20 entries, 15 skipped, 5 ermaining) 1 / 5 2 / 5 3 / 5 4 / 5 5 / 5 URLs processed: 0 The correct response should be: "URLs processed: 5". Obviously, I fail to understand something important, and that is not an acceptable option. :-) I assume this is some problem due to subshell calling, maybe because of "grep | while". If I set COUNT=100 before the loop, it'll be 100 after it finishes. How can I have the final value of $COUNT _outside_ the loop for the final status message? Note that $COUNT will receive error checking, so it won't end at the same value as $REMAINING if something goes wrong, i. e., the +1 won't happen if the processing step does not successfully terminate and leave a $? -eq 0. But I won't add that check unless I can get the correct value at the end... :-) -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...