From imura@af.airnet.ne.jp  Wed Jan 10 08:34:37 2001
Return-Path: <imura@af.airnet.ne.jp>
Received: from mail.af.airnet.ne.jp (mail.af.airnet.ne.jp [210.159.66.49])
	by hub.freebsd.org (Postfix) with ESMTP id C63AB37B400
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 10 Jan 2001 08:34:36 -0800 (PST)
Received: from mail.ryu16.com (tok318.airnet.ne.jp [210.159.89.62])
	by mail.af.airnet.ne.jp (8.8.8/3.6W/06/13/98-AF.AIRNET.NE.JP) with ESMTP id BAA109788
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 11 Jan 2001 01:34:34 +0900
Received: (from imura@localhost)
	by mail.ryu16.com (8.11.1/8.11.1) id f0AGWmU72130;
	Thu, 11 Jan 2001 01:32:48 +0900 (JST)
	(envelope-from imura)
Message-Id: <200101101634.BAA109788@mail.af.airnet.ne.jp>
Date: Thu, 11 Jan 2001 01:32:48 +0900 (JST)
From: imura@af.airnet.ne.jp
To: FreeBSD-gnats-submit@freebsd.org
Subject: /bin/sh problem : variable after pipeline won't be set
X-Send-Pr-Version: 3.2

>Number:         24228
>Category:       bin
>Synopsis:       when using /bin/sh, setting variable after pipeline won't work
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jan 10 08:40:00 PST 2001
>Closed-Date:    Sun Jan 14 14:27:33 PST 2001
>Last-Modified:  Sun Jan 14 14:28:54 PST 2001
>Originator:     Ryuichiro Imura
>Release:        FreeBSD 4.2-STABLE i386
>Organization:
>Environment:

FreeBSD 4.2-STABLE

>Description:

When using /bin/sh, setting variables after "|"
(in other words, I mean setting variables in a pipeline sequence),
will be ignored out of the pipeline sequence.

If it is a definition of /bin/sh, it's ok, I'm sorry,
otherwise I think it should be fixed.

>How-To-Repeat:

write a simple shell script like this:
-----------starts here----------
#/bin/sh

cat FILE | while read line ; do
	if [ $line = foo ]; then
		variable=bar
	fi
	echo $variable   <---- this will be printed
done

echo $variable           <---- this will NOT be printed
-----------ends here------------

>Fix:

Sorry, this is only a information of my problem.


>Release-Note:
>Audit-Trail:

From: Peter Pentchev <roam@orbitel.bg>
To: imura@af.airnet.ne.jp
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/24228: /bin/sh problem : variable after pipeline won't be set
Date: Sun, 14 Jan 2001 12:47:51 +0200

 On Thu, Jan 11, 2001 at 01:32:48AM +0900, imura@af.airnet.ne.jp wrote:
 > 
 > >Number:         24228
 > >Category:       bin
 > >Synopsis:       when using /bin/sh, setting variable after pipeline won't work
 > >Originator:     Ryuichiro Imura
 > >Release:        FreeBSD 4.2-STABLE i386
 > >Environment:
 > 
 > FreeBSD 4.2-STABLE
 > 
 > >Description:
 > 
 > When using /bin/sh, setting variables after "|"
 > (in other words, I mean setting variables in a pipeline sequence),
 > will be ignored out of the pipeline sequence.
 > 
 > If it is a definition of /bin/sh, it's ok, I'm sorry,
 > otherwise I think it should be fixed.
 
 AFAIK, yes, this is expected sh(1) behavior.  From the manpage:
 
      Note that unlike some other shells, sh executes each process in the
      pipeline as a child of the sh process.  Shell builtin commands are the
      exception to this rule.  They are executed in the current shell, although
      they do not affect its environment when used in pipelines.
 
 That is, sh(1) forks a subshell for each subsequent command/construct
 in the pipe, and variable assignments within a construct are only valid
 for the subshell that construct is executing in, and not back-propagated
 to the main shell.
 
 Thus, in your example, 'variable' would only be set to 'bar' for
 the subshell executing the while loop.  See below for a suggestion
 for a workaround.
 
 > >How-To-Repeat:
 > 
 > write a simple shell script like this:
 > -----------starts here----------
 > #/bin/sh
 > 
 > cat FILE | while read line ; do
 > 	if [ $line = foo ]; then
 > 		variable=bar
 > 	fi
 > 	echo $variable   <---- this will be printed
 > done
 > 
 > echo $variable           <---- this will NOT be printed
 > -----------ends here------------
 
 You may do better with something like the following:
 
 #!/bin/sh
 # (make sure that's #!, not just # ;)
 
 exec < FILE
 
 while read line; do
 	if [ "$line" = "foo" ]; then
 		variable="bar"
 	fi
 	echo "line is $line, variable is $variable"
 done
 
 echo "after the loop, variable is $variable"
 
 Hope that makes things clearer :)
 
 G'luck,
 Peter
 
 -- 
 Hey, out there - is it *you* reading me, or is it someone else?
 

From: "R. Imura" <imura@af.airnet.ne.jp>
To: Peter Pentchev <roam@orbitel.bg>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/24228: /bin/sh problem : variable after pipeline won't be set
Date: Mon, 15 Jan 2001 00:49:26 +0900

 On Sun, Jan 14, 2001 at 12:47:51PM +0200, Peter Pentchev wrote:
 > AFAIK, yes, this is expected sh(1) behavior.  From the manpage:
 > 
 >      Note that unlike some other shells, sh executes each process in the
 >      pipeline as a child of the sh process.  Shell builtin commands are the
 >      exception to this rule.  They are executed in the current shell, although
 >      they do not affect its environment when used in pipelines.
 > 
 > That is, sh(1) forks a subshell for each subsequent command/construct
 > in the pipe, and variable assignments within a construct are only valid
 > for the subshell that construct is executing in, and not back-propagated
 > to the main shell.
 > 
 > Thus, in your example, 'variable' would only be set to 'bar' for
 > the subshell executing the while loop.  See below for a suggestion
 > for a workaround.
 
 Thanks very much for your explanation. I got it. :)
 So, please close this PR.
 
 - R. Imura
 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Sun Jan 14 14:27:33 PST 2001 
State-Changed-Why:  
Satisfactory explaination provided by Peter Pentchev. 

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