Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 May 2014 14:48:22 -0400
From:      Rick Miller <vmiller@hostileadmin.com>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Bourne variable unset outside while()
Message-ID:  <CAHzLAVFhyPtkWCj1uj0aq0AusfrWL55UUn-mkxiyzUus7x4vdA@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
Hi all,

I have a Bourne shell script that echoes the contents of a data structure
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 while(),
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.

/** The script **/

#! /bin/sh -x

fs="freebsd-ufs gprootfs 1G
freebsd-swap gpswapfs 1G
freebsd-ufs gpvarfs 1G";

echo "${fs}" |
while read -r fstype fslabel fssize; do

        labels="${labels} ${fslabel}";
        echo "${labels}";

done

echo -e "\nlabels = ${labels}";

/** End the script **/

The output from this script is at http://pastebin.com/mxNLLWtm

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 right
direction.

-- 
Take care
Rick Miller



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