From owner-freebsd-questions@FreeBSD.ORG Thu Apr 4 15:11:01 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 649E4824; Thu, 4 Apr 2013 15:11:01 +0000 (UTC) (envelope-from Devin.Teske@fisglobal.com) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) by mx1.freebsd.org (Postfix) with ESMTP id 34148901; Thu, 4 Apr 2013 15:11:01 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.31]) by ltcfislmsgpa06.fnfis.com (8.14.5/8.14.5) with ESMTP id r34FAwoR007322 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 4 Apr 2013 10:10:58 -0500 Received: from LTCFISWMSGMB21.FNFIS.com ([10.132.99.23]) by LTCFISWMSGHT03.FNFIS.com ([10.132.206.31]) with mapi id 14.02.0309.002; Thu, 4 Apr 2013 10:10:58 -0500 From: "Teske, Devin" To: Mark Felder Subject: Re: OT: posix sh problem Thread-Topic: OT: posix sh problem Thread-Index: AQHOMTCBUnKVZwodmkSvNRb8MTRBZJjGaUsAgAANh4CAAAYVgIAAAcGA Date: Thu, 4 Apr 2013 15:10:57 +0000 Message-ID: <13CA24D6AB415D428143D44749F57D7201EF2689@ltcfiswmsgmb21> References: <13CA24D6AB415D428143D44749F57D7201EF2411@ltcfiswmsgmb21> <13CA24D6AB415D428143D44749F57D7201EF2627@ltcfiswmsgmb21> In-Reply-To: <13CA24D6AB415D428143D44749F57D7201EF2627@ltcfiswmsgmb21> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.132.253.121] Content-Type: text/plain; charset="Windows-1252" Content-ID: <7B82C67C5BEE1A469A14C47B1B327185@fisglobal.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8626, 1.0.431, 0.0.0000 definitions=2013-04-04_06:2013-04-04,2013-04-04,1970-01-01 signatures=0 Cc: Devin Teske , FreeBSD Questions X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Devin Teske List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Apr 2013 15:11:01 -0000 Oh, and just to cover all bases=85 If you suspect you have sub-shells in the loop, use "export" to export the = vars so that the sub-shells get the vars in the loop. --=20 Devin On Apr 4, 2013, at 8:04 AM, Teske, Devin wrote: >=20 > On Apr 4, 2013, at 7:42 AM, Mark Felder wrote: >=20 >> On Thu, 04 Apr 2013 08:54:30 -0500, Teske, Devin wrote: >>=20 >>> Wait, you can't? Then I've been doing something wrong all these years=85 >>> #!/bin/sh >>> printf "line1\nline2\n" | while read line >>> do >>> echo "line=3D[$line]" >>> done >>=20 >> You sort-of can, but it's not portable at all. As detailed here: http://= www.etalabs.net/sh_tricks.html >>=20 >>>> One common pitfall is trying to read output piped from commands, such = as: >>>> foo | IFS=3D read var >>>> POSIX allows any or all commands in a pipeline to be run in subshells, >=20 > And for most purposes that's fine. Read-on=85 >=20 >=20 >>>> and which command (if any) runs in the main shell varies greatly betwe= enimplementations >=20 > =85 that is only if you truly need the variables to be read into the main= shell. This is most always not what you want. >=20 > The page you linked about doesn't talk about the special case of "while",= in example: >=20 > foo | IFS=3D while read var >=20 > On the back-end nothing changes (the same caveat applies -- variables set= on the right side of the pipe may not be available to the main shell; as-p= er the quoted text). However, the high-level task changes from: >=20 > I want to read some text from a pipe into some variables >=20 > to instead: >=20 > I want to read some text from a pipe and process it word-by-word (in you= r case) and act on the words in a loop >=20 > So in other words=85 the only reason for wanting the variables in the mai= n shell is if you want to act on the last set of variables for the last lin= e after the loop has run (and presumably already processed the last line). = This is what I am saying anyone will rarely ever want. In other words, once= the loop (potentially running in a sub-shell) has completed, you likely do= n't care about the variable contents and are willing to throw them away any= how. >=20 >=20 >=20 >>>> =97 in particular Bash and ksh differ here. The standardidiom for over= coming this problem is to use a here document: >>>>=20 >>>> IFS=3D read var << EOF >>>> $(foo) >>>> EOF >>=20 >=20 > But you're not processing a single line; you're processing the entire inp= ut at-once and performing an action (writing to the screen) that also doesn= 't care whether it's in a sub-shell or not. >=20 > SO=85 >=20 > I say rock-on with the original syntax. It's portable. You don't need tho= se vars when the loop ends. >=20 >>=20 >> I was having problems with the variables magically becoming empty, remem= bered I had Rich's site bookmarked, checked to see if it mentioned and it w= as. I'll admit there's a high chance that due to lack of sleep user error w= as the culprit. >=20 > I'm interested in why you need the variables after the loop has completed= . Put your code in the loop where the variables are defined and have values. > --=20 > Devin >=20 > _____________ > The information contained in this message is proprietary and/or confident= ial. If you are not the intended recipient, please: (i) delete the message = and all copies; (ii) do not disclose, distribute or use the message in any = manner; and (iii) notify the sender immediately. In addition, please be awa= re that any message addressed to our domain is subject to archiving and rev= iew by persons other than the intended recipient. Thank you. _____________ The information contained in this message is proprietary and/or confidentia= l. If you are not the intended recipient, please: (i) delete the message an= d all copies; (ii) do not disclose, distribute or use the message in any ma= nner; and (iii) notify the sender immediately. In addition, please be aware= that any message addressed to our domain is subject to archiving and revie= w by persons other than the intended recipient. Thank you.