Date: Thu, 8 May 2014 17:18:18 -0400 From: Rick Miller <vmiller@hostileadmin.com> To: Polytropon <freebsd@edvax.de> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: Bourne variable unset outside while() Message-ID: <CAHzLAVETmYjt6Az0q519v41g6RGmEV8A%2BAEC2fbZdBtM3wFAMw@mail.gmail.com> In-Reply-To: <20140508225459.83f70b3c.freebsd@edvax.de> References: <CAHzLAVFhyPtkWCj1uj0aq0AusfrWL55UUn-mkxiyzUus7x4vdA@mail.gmail.com> <20140508225459.83f70b3c.freebsd@edvax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
Thanks, Polytropon & Matthew! On Thu, May 8, 2014 at 4:54 PM, Polytropon <freebsd@edvax.de> wrote: > > You have identified the problem: With the | redirector, a subshell > is started which populates ${labels}, but at the "higher" level > ${labels} keeps its setting (in this case, implicit empty string). > > Here's a workaround, using awk: > > #!/bin/sh > > fs="freebsd-ufs gprootfs 1G > freebsd-swap gpswapfs 1G > freebsd-ufs gpvarfs 1G" > > labels=`echo "${fs}" | awk '{printf("%s ", $2);}'` > echo "labels = ${labels}" > > In this example, with `...`, which is the same as $(...), also > starts a subshell, but assigns its output to a variable, so there > will be no scope problem. This idea uses the advantage that your > delimiter is newline, and awk can process the input line per line, > printing "incomplete lines" (no newline added) as output. > > The result will be: > > labels = gprootfs gpswapfs gpvarfs > > Is this what you expected? > Wow! It's soooo obvious to me now that you've pointed it out. I don't often use while read's in this manner. Thanks for the feedback! -- Take care Rick Miller
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAHzLAVETmYjt6Az0q519v41g6RGmEV8A%2BAEC2fbZdBtM3wFAMw>