From owner-freebsd-questions@FreeBSD.ORG Thu May 8 21:18:18 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id EC991574 for ; Thu, 8 May 2014 21:18:18 +0000 (UTC) Received: from mail-ig0-x22b.google.com (mail-ig0-x22b.google.com [IPv6:2607:f8b0:4001:c05::22b]) (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 BA95A68D for ; Thu, 8 May 2014 21:18:18 +0000 (UTC) Received: by mail-ig0-f171.google.com with SMTP id c1so339687igq.10 for ; Thu, 08 May 2014 14:18:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=9FjMXRZsUTXVrC/YEuZA7oFS7ptRt3fmpKsmrkl3Ih8=; b=al4PhwuaCvFZ8oMhOp7pD/n4jEymYFJMsyYfXgViFpR7I8eI7PY5BJXdfZ3ksFGxyd 94KHFwcoOd3LSWMnWwTQnANX4T19q4B23wQWxTsD/srRJaOiL6TGaw8qKKJne1xSw+zL pbgSSGsS0byNoKw1WOgLH18w652Z5qK95bkFIJ/qINm5FLy8b/oWoV+8ljfqslblwzLo 3UIt+mrh2Y5TqNqoK5DG+0gQY3ILoLH6KV+eKx9jtAI6i8V6+XpAD3wi3xJoOsLxWevF TOGT4ojg7rUCtmOaio42C1cAkRIq9BWM9UnGGZ6NCLVcmFm/T6iYcJ9grK129e03PBed 8XlQ== MIME-Version: 1.0 X-Received: by 10.50.109.130 with SMTP id hs2mr165562igb.29.1399583898257; Thu, 08 May 2014 14:18:18 -0700 (PDT) Sender: vrwmiller@gmail.com Received: by 10.64.235.212 with HTTP; Thu, 8 May 2014 14:18:18 -0700 (PDT) In-Reply-To: <20140508225459.83f70b3c.freebsd@edvax.de> References: <20140508225459.83f70b3c.freebsd@edvax.de> Date: Thu, 8 May 2014 17:18:18 -0400 X-Google-Sender-Auth: OKXgtYcnJKYcviW5zCFunOQDwl4 Message-ID: Subject: Re: Bourne variable unset outside while() From: Rick Miller To: Polytropon Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.18 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 21:18:19 -0000 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="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