From kogoe@japan.sun.com  Fri Nov 22 01:22:27 2002
Return-Path: <kogoe@japan.sun.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 11C8937B401
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 22 Nov 2002 01:22:26 -0800 (PST)
Received: from kathmandu.sun.com (kathmandu.sun.com [192.18.98.36])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 66AA743EA3
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 22 Nov 2002 01:22:25 -0800 (PST)
	(envelope-from kogoe@japan.sun.com)
Received: from cedar.Japan.Sun.COM ([129.158.31.243])
	by kathmandu.sun.com (8.9.3+Sun/8.9.3) with ESMTP id CAA28164
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 22 Nov 2002 02:22:24 -0700 (MST)
Received: from vaio.Japan.Sun.COM (eslab203 [129.158.47.203])
	by cedar.Japan.Sun.COM (8.10.2+Sun/8.10.2/ENSMAIL,v2.2) with ESMTP id gAM9MMx03858;
	Fri, 22 Nov 2002 18:22:22 +0900 (JST)
Received: from vaio.Japan.Sun.COM (localhost [127.0.0.1])
	by vaio.Japan.Sun.COM (8.12.6/8.12.6) with ESMTP id gAM9MMjR095650;
	Fri, 22 Nov 2002 18:22:22 +0900 (JST)
	(envelope-from kogoe@vaio.Japan.Sun.COM)
Received: (from kogoe@localhost)
	by vaio.Japan.Sun.COM (8.12.6/8.12.6/Submit) id gAM9MMcw095649;
	Fri, 22 Nov 2002 18:22:22 +0900 (JST)
Message-Id: <200211220922.gAM9MMcw095649@vaio.Japan.Sun.COM>
Date: Fri, 22 Nov 2002 18:22:22 +0900 (JST)
From: Hideo Kogoe <Hideo.Kogoe@Sun.COM>
Reply-To: Hideo Kogoe <Hideo.Kogoe@Sun.COM>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Hideo Kogoe <Hideo.Kogoe@Sun.COM>
Subject: read builtin function of sh does not read standard input
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         45584
>Category:       bin
>Synopsis:       read builtin function of sh does not read standard input
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Nov 22 01:30:01 PST 2002
>Closed-Date:    Tue Dec 20 15:59:22 GMT 2005
>Last-Modified:  Tue Dec 20 15:59:22 GMT 2005
>Originator:     Hideo Kogoe
>Release:        FreeBSD 4.7-RELEASE i386
>Organization:
Sun Microsystems K.K.
>Environment:
System: FreeBSD vaio.Japan.Sun.COM 4.7-RELEASE FreeBSD 4.7-RELEASE #0: Mon Nov 18 14:46:18 JST 2002 root@vaio.Japan.Sun.COM:/usr/src/sys/compile/VAIO i386

	SONY VAIO PCG-R505R/GK running a customer kernel

>Description:
	The read builtin function of /bin/sh does not read the
	standard input correctly.  Refer to the How-To-Repeat
	section.

>How-To-Repeat:
	$ echo Hello | read line; echo $line

	$ echo Hello | while read line; do echo $line; done
	Hello
	$

    Just for reference, on Solaris, for example,

	$ echo Hello | read line; echo $line
	Hello
	$ echo Hello | while read line; do echo $line; done
	Hello
	$ 

    Accroding to the specification of POSIX shell, the read
    function doesn't behave correctly.

>Fix:
	Putting them into a sub-shell seems to work fine. 

	$ echo Hello | (read line; echo $line)
	Hello
	$ 
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->tjr 
Responsible-Changed-By: mike 
Responsible-Changed-When: Sun Dec 1 07:39:06 PST 2002 
Responsible-Changed-Why:  
Over to tjr; /bin/sh conformance issue. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=45584 
State-Changed-From-To: open->analyzed 
State-Changed-By: tjr 
State-Changed-When: Mon Dec 2 15:56:22 PST 2002 
State-Changed-Why:  
This happens because the shell forks a child for each command in the 
pipeline. The "line" variable in your first example is actually being 
set to the value from standard input, but changes to variables in the 
child process have no effect on their contents in the parent process. 

This is similar to bin/34811. I don't know an elegant way of solving it 
short of using rfork_thread(3). I'll investigate how pdksh and bash 
manage to do it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=45584 
Responsible-Changed-From-To: tjr->freebsd-bugs 
Responsible-Changed-By: tjr 
Responsible-Changed-When: Sat Feb 14 23:38:10 PST 2004 
Responsible-Changed-Why:  
Unassign due to lack of time and interest. Perhaps someone else 
will pick this up. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=45584 
State-Changed-From-To: analyzed->suspended 
State-Changed-By: stefanf 
State-Changed-When: Thu Dec 8 19:16:48 GMT 2005 
State-Changed-Why:  
Suspend this PR as the current behaviour (while not exactly desirable) is 
allowed by POSIX and isn't really a bug.  Of course a nice patch would be 
gladly accepted. 

# 2.12 Shell Execution Environment 
#  
# Additionally, each command of a multi-command pipeline is in a subshell 
# environment; as an extension, however, any or all commands in a pipeline may 
# be executed in the current environment. 

This means forking, executing read and setting variables in a sub-shell is 
allowed. 

Other shells implement this extension, FreeBSD's does not. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=45584 

From: Oliver Fromme <olli@lurza.secnetix.de>
To: bug-followup@FreeBSD.org, Hideo.Kogoe@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

From: Oliver Fromme <olli@lurza.secnetix.de>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/45584: read builtin function of sh does not read standard input
Date: Tue, 20 Dec 2005 15:08:42 +0100 (CET)

 Small note:  I just noticed that this PR is
 already > 3 years old.  Furthermore, e-mail
 to the Originator bounces with "user unkown".
 
 More reasons to close it, IMHO.  :-)
 
 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.
 
 "To this day, many C programmers believe that 'strong typing'
 just means pounding extra hard on the keyboard."
         -- Peter van der Linden
State-Changed-From-To: suspended->closed 
State-Changed-By: linimon 
State-Changed-When: Tue Dec 20 15:58:46 UTC 2005 
State-Changed-Why:  
Submitter's email address bounces. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=45584 
>Unformatted:
