From owner-freebsd-questions@FreeBSD.ORG Thu May 8 21:07:38 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id EBAE0F50 for ; Thu, 8 May 2014 21:07:38 +0000 (UTC) Received: from smtp.infracaninophile.co.uk (smtp6.infracaninophile.co.uk [IPv6:2001:8b0:151:1:3cd3:cd67:fafa:3d78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "smtp.infracaninophile.co.uk", Issuer "ca.infracaninophile.co.uk" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 94F0739C for ; Thu, 8 May 2014 21:07:38 +0000 (UTC) Received: from seedling.black-earth.co.uk (seedling.black-earth.co.uk [81.2.117.99]) (authenticated bits=0) by smtp.infracaninophile.co.uk (8.14.8/8.14.8) with ESMTP id s48L7QY9069997 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO) for ; Thu, 8 May 2014 22:07:26 +0100 (BST) (envelope-from matthew@FreeBSD.org) Authentication-Results: lucid-nonsense.infracaninophile.co.uk; dmarc=none header.from=FreeBSD.org DKIM-Filter: OpenDKIM Filter v2.8.3 smtp.infracaninophile.co.uk s48L7QY9069997 Authentication-Results: smtp.infracaninophile.co.uk/s48L7QY9069997; dkim=none reason="no signature"; dkim-adsp=none Message-ID: <536BF206.4060005@FreeBSD.org> Date: Thu, 08 May 2014 22:07:18 +0100 From: Matthew Seaman User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: freebsd-questions@freebsd.org Subject: Re: Bourne variable unset outside while() References: In-Reply-To: X-Enigmail-Version: 1.6 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="FAwdqdd3CaBRhBeGXHEf5G8ps26bFKGjj" X-Virus-Scanned: clamav-milter 0.98.1 at lucid-nonsense.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-3.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lucid-nonsense.infracaninophile.co.uk X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 May 2014 21:07:39 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --FAwdqdd3CaBRhBeGXHEf5G8ps26bFKGjj Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 08/05/2014 19:48, Rick Miller wrote: > I have a Bourne shell script that echoes the contents of a data structu= re > to a while() loop that iterates over the data and is expected to append= a > string from the current iteration to a different list. Inside the whil= e(), > it works fine, but echo'ing the list outside the loop produces empty > output. I expect the variable to maintain it's final state after > processing the while(), but it does not appear to be doing so. >=20 > /** The script **/ >=20 > #! /bin/sh -x >=20 > fs=3D"freebsd-ufs gprootfs 1G > freebsd-swap gpswapfs 1G > freebsd-ufs gpvarfs 1G"; >=20 > echo "${fs}" | > while read -r fstype fslabel fssize; do >=20 > labels=3D"${labels} ${fslabel}"; > echo "${labels}"; >=20 > done >=20 > echo -e "\nlabels =3D ${labels}"; >=20 > /** End the script **/ >=20 > The output from this script is at http://pastebin.com/mxNLLWtm >=20 > This almost appears to be an issue with scope, but I've not encountered= > this before, especially in such simple context. I must be missing > something obvious and just looking for feedback to send me off in the r= ight > direction. Piping input to a 'while read' in shell implies that the content of the while loop will be run in a sub-shell. Meaning it can't affect the value of any variables in the parent process, ie. outside the while loop.= This is a fairly obscure gotcha in shell programming -- finding out about it is like one of those rite of passage on the way to shell-godhood= =2E Either put the tail of your script inside the while loop, using some sort of if- condition or other wise arranging for it to only run the last time around the loop, or print the values you need as a statement you can eval, or use a temporary file or ... Well, there are many ways to solve this problem. Good luck and have fun. Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. PGP: http://www.infracaninophile.co.uk/pgpkey --FAwdqdd3CaBRhBeGXHEf5G8ps26bFKGjj Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.20 (Darwin) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQJ8BAEBCgBmBQJTa/IOXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25zLm9w ZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXQ2NTNBNjhCOTEzQTRFNkNGM0UxRTEzMjZC QjIzQUY1MThFMUE0MDEzAAoJELsjr1GOGkATFecQAIb2b8ydCv0sbK+cAWVb9UV/ s3dTlfdr7oY7Mij+qW4wVnrzFBP+i3mAVI36rhRknZgYYEjTDqIpL6Zif9rZ2LA8 /g4BFEgPLKTj6VCYBFzjQxf7SJEc4yQItdKFDkhOf7NE+d7R3zvGVGL0rdJo29p6 8XiZascBenyS7T/o1RlyaNAzjoGNNq3ZbsiMfwkncVfje5m1Xe8BvEWfPhW9Xg9s eoZKTI9bZLNPxtvhcYEfQZ8oCahCroNEyQsxqu3Zj5MmUutlglQNs3KB6SaqfKP3 QQ6/hN5T8EwT5WgtOLCK/Q9PeI9Ml4vwEpnuD5dcWGICc3FZI3SzZXwW3PKOhF/J Mov5SEfeZPCx6hmK+FG28ODnl52Oa+3IG5oCaYAxulV4Z+rzTwWu5nS8+mdMDokR RI3jLSyblYP/NxH+T1phLgddRAcSt+eCqWKjnWcVPxhuqN5rC/UIF5/UgXalkcBN iLqPpNidUbpei68R+k+hvCq/ih7cZdooiAMw8kIuvVnC8D/43pikjUru+84luCdn 4yN1HI4VM2HN6+PVKxmj/X8ZWzYCNO+o25g0bNNr9/TnsM5g4lWiipQRsNZXWJO7 viNnE/49cJfiKJUCqyKBIM8Cw0YLA76xZY2V1JANsEEsrbQ6nIAmHec+KydKE6z7 IdhGVmAn0czBiFdIroyO =8DzM -----END PGP SIGNATURE----- --FAwdqdd3CaBRhBeGXHEf5G8ps26bFKGjj--