From owner-freebsd-questions@FreeBSD.ORG Thu May 8 22:23:59 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 DD5FC5FB for ; Thu, 8 May 2014 22:23:58 +0000 (UTC) Received: from mail-qc0-x22f.google.com (mail-qc0-x22f.google.com [IPv6:2607:f8b0:400d:c01::22f]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 99F1CC2D for ; Thu, 8 May 2014 22:23:58 +0000 (UTC) Received: by mail-qc0-f175.google.com with SMTP id w7so3698602qcr.34 for ; Thu, 08 May 2014 15:23:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:content-transfer-encoding:message-id:date :subject:from:in-reply-to:references:to:cc; bh=Uq4s/PS9zWv6sLnnaWPNKqc11rkphdr0+51SdYnbz8w=; b=tLVnR2+EMu39UAskgh0vQ5fLQqapV0ryE7DusHH0oxje71WZlnOgE8z6alsW7n7t00 37ih1wAHFPN2SCvgKe20sWOew3LD5EPHP3PYsFMYuDyXsGBHYO01SPNA45iYYiiANDZL SkeU0hZTejblyONbAMKL/rE80AinNbTyf7hWzt0diHG7RsAVEMn8iUXRfWzCgmSTqUVr C0pTrktgJPVr4yyiKyNXPueGf7nFDxw2ukaCWgDu0GARqyYlym6uFRNZUPQhYgkYtNaz 8lexUALY1njSYFJ9jste/WJnVx0uENUfjEVV5BVFeMkDMZamfgXYgd+/3p28aWFzJFmZ FaMg== X-Received: by 10.224.113.205 with SMTP id b13mr9313495qaq.19.1399587837807; Thu, 08 May 2014 15:23:57 -0700 (PDT) Received: from [127.0.0.1] ([2600:1003:b12d:fd6e:941a:b447:7ab6:d82c]) by mx.google.com with ESMTPSA id f7sm3460588qar.42.2014.05.08.15.23.55 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 08 May 2014 15:23:56 -0700 (PDT) Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: BlackBerry Email (10.2.1.2174) Message-ID: <20140508222404.5898387.62898.7104@gmail.com> Date: Thu, 08 May 2014 18:24:04 -0400 Subject: Re: Bourne d unset outside while() From: monahbaki@gmail.com In-Reply-To: References: <20140508225459.83f70b3c.freebsd@edvax.de> To: Rick Miller , Polytropon , Erich Dollansky Cc: 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: Thu, 08 May 2014 22:23:59 -0000 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 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"