From owner-freebsd-questions@freebsd.org Wed Dec 4 19:41:09 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 8CD401ACDCB for ; Wed, 4 Dec 2019 19:41:09 +0000 (UTC) (envelope-from lobo@bsd.com.br) Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47Sq374sDpz4HFc for ; Wed, 4 Dec 2019 19:41:07 +0000 (UTC) (envelope-from lobo@bsd.com.br) Received: by mail-il1-x131.google.com with SMTP id b15so670368iln.3 for ; Wed, 04 Dec 2019 11:41:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsd.com.br; s=capeta; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=TaGwbBobd9GAy/vs+61PKUPjhWvc5RkqX7ZzLhjCF6I=; b=R3d4WfnOuDPLsTj7N/4Q5mnZ5MOS3qPYzepUyj0XdgMTAdwqv50yyDvFD+E8UqdECP d5hLcgE39xLTZ9/BJdiR4c1JtXqsy7mdOHdtkCJcQrSKv6Xj1Eui9ovvylNo1nfgPDlc Ci9GRdjTcyDKZZEAA+ygz6SaEOkEqhyaKS9+g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=TaGwbBobd9GAy/vs+61PKUPjhWvc5RkqX7ZzLhjCF6I=; b=ZOnoX5kK5mHi71Nh68RsgUUOohnC4ncqePE/U9iaDjVxJ0ddQxRbdjT3hYVm1duQ04 LDYO0jUAS7d/xfkYRwmS7WBIAQkuhnjSugD6UXaYXVM/zW3g0KEO2PWdCi7hiDSmT8Sv lWUs02tnpNHXtZvrTqHFb/G20o7WU8u2GXKUo25lFUsh1E265IA9p8YowRXvTCUsV9nv AtkSBb3GrWxsdR0xUqZ6jG6BY+Iy+MOGDk7CcNEhJ7x2SgWapVdgWXGfqBCIPnmtLYQa sjVB4a3rdS+AVDOhHuXv08OurYqMScTPFtlFNGFU7zALfgMgQF4D90lsc3DNkQgL4O6+ VqHg== X-Gm-Message-State: APjAAAWBCSDOej+KW1f4yhxPf0tmb2G+UpFFgoWrl9D7TFFjIk8GCSLa NQUromf1sROe6Cl6HR9Hgb/4morf+LRZuEqj/sjwuuG+a+w= X-Google-Smtp-Source: APXvYqxtpxtqaEOwoqe3SNN6HpuQ12EKZ7jhrKqSGW3x6itUTsHM7ZudX/7Ff7FxiN0AukqSjqYiTwldw2PdTXKXdpU= X-Received: by 2002:a92:413:: with SMTP id 19mr4889057ile.117.1575488466248; Wed, 04 Dec 2019 11:41:06 -0800 (PST) MIME-Version: 1.0 References: <20191204181300.8dd0e03c.freebsd@edvax.de> In-Reply-To: <20191204181300.8dd0e03c.freebsd@edvax.de> From: Mario Lobo Date: Wed, 4 Dec 2019 16:39:36 -0300 Message-ID: Subject: Re: Counter in sh inside loop, value "encapsulation" To: FreeBSD Questions X-Rspamd-Queue-Id: 47Sq374sDpz4HFc X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=bsd.com.br header.s=capeta header.b=R3d4WfnO; dmarc=none; spf=pass (mx1.freebsd.org: domain of lobo@bsd.com.br designates 2607:f8b0:4864:20::131 as permitted sender) smtp.mailfrom=lobo@bsd.com.br X-Spamd-Result: default: False [-4.95 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[bsd.com.br:s=capeta]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; DMARC_NA(0.00)[bsd.com.br]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[bsd.com.br:+]; RCVD_IN_DNSWL_NONE(0.00)[1.3.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; IP_SCORE(-2.45)[ip: (-8.04), ipnet: 2607:f8b0::/32(-2.23), asn: 15169(-1.93), country: US(-0.05)]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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 19:41:09 -0000 On Wed, Dec 4, 2019 at 2:13 PM Polytropon wrote: > 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, ... > _______________________________________________ > freebsd-questions@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to " > freebsd-questions-unsubscribe@freebsd.org" > Could you try that? #!/usr/local/bin/bash # ... # ... lots of boring stuff omitted ... # ... echo "Start: ${TIMESTAMP}" echo "Input: ${INFILE} (${TOTAL} entries, ${SKIPPED} skipped, ${REMAINING} remaining)" declare -a array=(`cat ${INFILE}`) arraylength=${#array[@]} COUNT=1 for (( i=1; i<${arraylength}; i++ )); do grep "^https" ${array[$i]} echo echo $COUNT"/"$arraylength # echo "---> ${URL}" # ... # ... processing per URL, also boring ... # ... COUNT=$((COUNT+1)) done echo echo "URLs processed:" $COUNT echo -n "Time: ${TIMESTAMP} - " date "+%Y-%m-%d %H%:%M:%S" echo "" exit 0 -- Mario Lobo http://www.mallavoodoo.com.br FreeBSD since version 2.2.8 [not Pro-Audio.... YET!!]