Bourne variable unset outside while()

Rick Miller vmiller at hostileadmin.com
Fri May 9 14:57:06 UTC 2014


On Fri, May 9, 2014 at 10:45 AM, Polytropon <freebsd at edvax.de> wrote:

> On Fri, 9 May 2014 06:42:45 +0000, Ivailo A. Tanusheff wrote:
> > Hi,
> >
> > I think you can check out this:
> > http://stackoverflow.com/questions/7482510/variable-incrementing-in-bash
> >
> > So I sugest you do the same trick or use different approach - awk or
> something like this.
>
> This actually works (and is a good idea to get rid of my suggested
> `awk ...` call per each line of input). In "here documents", variable
> expansion can be used. If the input will be coming from a file
> instead, using < /the/file can be done.
>
>         #!/bin/sh
>
>         fs="freebsd-ufs gprootfs 1G
>         freebsd-swap gpswapfs 1G
>         freebsd-ufs gpvarfs 1G"
>
>         while read -r fstype fslabel fssize; do
>                 labels="${labels} ${fslabel}"
>         done << EOF
>         "${fs}"
>         EOF
>
>         echo "labels = ${labels}"
>
> The result is:
>
>         labels =  gprootfs gpswapfs gpvarfs
>
> There's a leading space because at the first addition, ${labels}
> is empty, a space and the 1st entry are then added. The awk approach
> didn't have that "bug", erm... feature. ;-)
>

Yes, this is a more efficient approach.  Thanks for providing further input.


-- 
Take care
Rick Miller


More information about the freebsd-questions mailing list