Date: Thu, 08 May 2014 18:24:04 -0400 From: monahbaki@gmail.com To: Rick Miller <vmiller@hostileadmin.com>, Polytropon <freebsd@edvax.de>, Erich Dollansky <erichsfreebsdlist@alogt.com> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: Bourne d unset outside while() Message-ID: <20140508222404.5898387.62898.7104@gmail.com> In-Reply-To: <CAHzLAVETmYjt6Az0q519v41g6RGmEV8A%2BAEC2fbZdBtM3wFAMw@mail.gmail.com> References: <CAHzLAVFhyPtkWCj1uj0aq0AusfrWL55UUn-mkxiyzUus7x4vdA@mail.gmail.com> <20140508225459.83f70b3c.freebsd@edvax.de> <CAHzLAVETmYjt6Az0q519v41g6RGmEV8A%2BAEC2fbZdBtM3wFAMw@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
K Szo Sent from my BlackBerry 10 smartphone on th e Verizon Wireless 4G LTE network. =A0 Original Message =A0 From: Rick Miller Sent: Thursday, May 8, 2014 5:18 PM To: Polytropon Cc: FreeBSD Question ns Subject: Re: Bourne variable unset outside while() 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=3D"freebsd-ufs gprootfs 1G > freebsd-swap gpswapfs 1G > freebsd-ufs gpvarfs 1G" > > labels=3D`echo "${fs}" | awk '{printf("%s ", $2);}'` > echo "labels =3D ${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 =3D 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! --=20 Take care Rick Miller _______________________________________________ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140508222404.5898387.62898.7104>