From owner-freebsd-questions@FreeBSD.ORG Thu Apr 4 15:04:56 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 02AEF6FB; Thu, 4 Apr 2013 15:04:56 +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 C6C8F8BA; Thu, 4 Apr 2013 15:04:55 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.16]) by ltcfislmsgpa01.fnfis.com (8.14.5/8.14.5) with ESMTP id r34F4MII006865 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 4 Apr 2013 10:04:50 -0500 Received: from LTCFISWMSGMB21.FNFIS.com ([10.132.99.23]) by LTCFISWMSGHT05.FNFIS.com ([10.132.206.16]) with mapi id 14.02.0309.002; Thu, 4 Apr 2013 10:04:42 -0500 From: "Teske, Devin" To: Mark Felder Subject: Re: OT: posix sh problem Thread-Topic: OT: posix sh problem Thread-Index: AQHOMTCBUnKVZwodmkSvNRb8MTRBZJjGaUsAgAANh4CAAAYVgA== Date: Thu, 4 Apr 2013 15:04:41 +0000 Message-ID: <13CA24D6AB415D428143D44749F57D7201EF2627@ltcfiswmsgmb21> References: <13CA24D6AB415D428143D44749F57D7201EF2411@ltcfiswmsgmb21> In-Reply-To: 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: <15621CB228B5A944A5152B30CB5C15F1@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 , "" 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:04:56 -0000 On Apr 4, 2013, at 7:42 AM, Mark Felder wrote: > 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://w= ww.etalabs.net/sh_tricks.html >=20 >>> One common pitfall is trying to read output piped from commands, such a= s: >>> foo | IFS=3D read var >>> POSIX allows any or all commands in a pipeline to be run in subshells, And for most purposes that's fine. Read-on=85 >>> and which command (if any) runs in the main shell varies greatly betwee= nimplementations =85 that is only if you truly need the variables to be read into the main s= hell. This is most always not what you want. The page you linked about doesn't talk about the special case of "while", i= n example: foo | IFS=3D while read var On the back-end nothing changes (the same caveat applies -- variables set o= n the right side of the pipe may not be available to the main shell; as-per= the quoted text). However, the high-level task changes from: I want to read some text from a pipe into some variables to instead: I want to read some text from a pipe and process it word-by-word (in your = case) and act on the words in a loop So in other words=85 the only reason for wanting the variables in the main = shell is if you want to act on the last set of variables for the last line = after the loop has run (and presumably already processed the last line). Th= is is what I am saying anyone will rarely ever want. In other words, once t= he loop (potentially running in a sub-shell) has completed, you likely don'= t care about the variable contents and are willing to throw them away anyho= w. >>> =97 in particular Bash and ksh differ here. The standardidiom for overc= oming this problem is to use a here document: >>>=20 >>> IFS=3D read var << EOF >>> $(foo) >>> EOF >=20 But you're not processing a single line; you're processing the entire input= at-once and performing an action (writing to the screen) that also doesn't= care whether it's in a sub-shell or not. SO=85 I say rock-on with the original syntax. It's portable. You don't need those= vars when the loop ends. >=20 > I was having problems with the variables magically becoming empty, rememb= ered I had Rich's site bookmarked, checked to see if it mentioned and it wa= s. I'll admit there's a high chance that due to lack of sleep user error wa= s the culprit. 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 _____________ 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.