Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 4 Apr 2013 15:10:57 +0000
From:      "Teske, Devin" <Devin.Teske@fisglobal.com>
To:        Mark Felder <feld@feld.me>
Cc:        Devin Teske <dteske@freebsd.org>, FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: OT: posix sh problem
Message-ID:  <13CA24D6AB415D428143D44749F57D7201EF2689@ltcfiswmsgmb21>
In-Reply-To: <13CA24D6AB415D428143D44749F57D7201EF2627@ltcfiswmsgmb21>
References:  <op.wu0nsgsf34t2sn@tech304.office.supranet.net> <13CA24D6AB415D428143D44749F57D7201EF2411@ltcfiswmsgmb21> <op.wu0tttba34t2sn@tech304.office.supranet.net> <13CA24D6AB415D428143D44749F57D7201EF2627@ltcfiswmsgmb21>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <Devin.Teske@fisglobal.=
com> 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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D7201EF2689>