Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 08 May 2014 22:07:18 +0100
From:      Matthew Seaman <matthew@FreeBSD.org>
To:        freebsd-questions@freebsd.org
Subject:   Re: Bourne variable unset outside while()
Message-ID:  <536BF206.4060005@FreeBSD.org>
In-Reply-To: <CAHzLAVFhyPtkWCj1uj0aq0AusfrWL55UUn-mkxiyzUus7x4vdA@mail.gmail.com>
References:  <CAHzLAVFhyPtkWCj1uj0aq0AusfrWL55UUn-mkxiyzUus7x4vdA@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?536BF206.4060005>