bin/45584: read builtin function of sh does not read standard
input
Oliver Fromme
olli at lurza.secnetix.de
Tue Dec 20 05:40:08 PST 2005
The following reply was made to PR bin/45584; it has been noted by GNATS.
From: Oliver Fromme <olli at lurza.secnetix.de>
To: bug-followup at FreeBSD.org, Hideo.Kogoe at Sun.COM
Cc:
Subject: Re: bin/45584: read builtin function of sh does not read standard input
Date: Tue, 20 Dec 2005 14:36:15 +0100 (CET)
Hi,
Actually it is well-known that parts of a pipeline are
usually executed in sub-shells, so setting variables
there has no effect. Scripts which try to do that are
non-portable.
In fact, most shells seem to use sub-shells: /bin/sh on
FreeBSD, pdksh, bash, and even the /bin/sh on Solaris.
I've found only two shells where "echo foo | read bar"
works: zsh and Solaris' ksh.
Therefore I think this PR should be closed.
There are several workarounds. For simple cases, just
use command substitution:
$ line=`echo Hello`
or:
$ line=$(echo Hello)
(The latter is preferred, because it is easier to nest
and works better with qwoting.)
For more complicated things, one possibility is to use
a "here-document" with command substitution. See the
sh(1) manpage for details.
Example: This is not portable and does _not_ work:
df -k / | awk '/%/{print $2, $3, $4}' | read a b c
echo "Total disk blocks: $a"
echo "Disk blocks used: $b"
echo "Disk blocks avail: $c"
Instead, the following works fine:
read a b c <<-END
$(df -k / | awk '/%/{print $2, $3, $4}')
END
echo "Total disk blocks: $a"
echo "Disk blocks used: $b"
echo "Disk blocks avail: $c"
Best regards
Oliver
--
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing
Dienstleistungen mit Schwerpunkt FreeBSD: http://www.secnetix.de/bsd
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.
"C++ is the only current language making COBOL look good."
-- Bertrand Meyer
More information about the freebsd-bugs
mailing list