From owner-freebsd-questions@FreeBSD.ORG Fri May 9 15:05:02 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 3EAB74F2 for ; Fri, 9 May 2014 15:05:02 +0000 (UTC) Received: from blue.qeng-ho.org (blue.qeng-ho.org [217.155.128.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id C9785EE2 for ; Fri, 9 May 2014 15:05:01 +0000 (UTC) Received: from fileserver.home.qeng-ho.org (localhost [127.0.0.1]) by fileserver.home.qeng-ho.org (8.14.7/8.14.5) with ESMTP id s49F4nnY090033; Fri, 9 May 2014 16:04:50 +0100 (BST) (envelope-from freebsd@qeng-ho.org) Message-ID: <536CEE91.7030202@qeng-ho.org> Date: Fri, 09 May 2014 16:04:49 +0100 From: Arthur Chance User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: Polytropon , "Ivailo A. Tanusheff" Subject: Re: Bourne variable unset outside while() References: <1422065A4E115F409E22C1EC9EDAFBA456552E@sofdc01exc02.postbank.bg> <20140509164546.d10312ee.freebsd@edvax.de> In-Reply-To: <20140509164546.d10312ee.freebsd@edvax.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Cc: Rick Miller , FreeBSD Questions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 May 2014 15:05:02 -0000 On 09/05/2014 15:45, Polytropon 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. ;-) You can avoid that leading space by using yet another obscure bit of shell programming. Make the loop body labels="${labels}${labels:+ }${fslabel}" The variable expansion ${foo:+bar} is empty if ${foo} is empty or unset, and "bar" is ${foo} is set and non-empty. Also useful in things like somecmd ${outfile:+-o} ${outfile} args ...