OT: posix sh problem
Mark Felder
feld at feld.me
Thu Apr 4 14:42:57 UTC 2013
On Thu, 04 Apr 2013 08:54:30 -0500, Teske, Devin
<Devin.Teske at fisglobal.com> wrote:
> Wait, you can't? Then I've been doing something wrong all these years…
> #!/bin/sh
> printf "line1\nline2\n" | while read line
> do
> echo "line=[$line]"
> done
You sort-of can, but it's not portable at all. As detailed here:
http://www.etalabs.net/sh_tricks.html
>> One common pitfall is trying to read output piped from commands, such
>> as:
>> foo | IFS= read var
>>POSIX allows any or all commands in a pipeline to be run in subshells,
>> and which command (if any) runs in the main shell varies greatly between
>> implementations — in particular Bash and ksh differ here. The standard
>> idiom for overcoming this problem is to use a here document:
>>
>> IFS= read var << EOF
>> $(foo)
>> EOF
I was having problems with the variables magically becoming empty,
remembered I had Rich's site bookmarked, checked to see if it mentioned
and it was. I'll admit there's a high chance that due to lack of sleep
user error was the culprit.
More information about the freebsd-questions
mailing list