zsh-workers
 help / color / mirror / code / Atom feed
* local variable assignment and pipelines?
@ 2003-07-18 21:58 Dan Nelson
  2003-07-18 23:20 ` Philippe Troin
  2003-07-21  9:59 ` Peter Stephenson
  0 siblings, 2 replies; 3+ messages in thread
From: Dan Nelson @ 2003-07-18 21:58 UTC (permalink / raw)
  To: zsh-workers


A coworker came to me with a scripting question, but after answering
his question I looked at it again and wondered why his command even
worked in the first place!  Here's his command:

rs=`fmt_layout -l bmg400.org.afmt -r` | fs=`ls -l /io1/auto/bmg/EMSSND03.org | awk '{ print $5 }'` | recs=`expr $fs / $rs` | rem=`expr $fs % $rs` | echo $rem

(basically it gets the record length and filesize of a fixed-width text
file, then computes the number of records and extra bytes)

Obviously, all those pipes (except the ls | awk one) should be
semicolons.  The big question is why are all the local variables set in
the pipeline visible to the final command?  Is it because zsh realizes
all the commands are variable assignments and runs them in serial in
the parent process?  That's what a ktrace seems to indicate is
happening.

It's sort of a neat feature, but is this supposed to happen?  I know
zsh guarantees that the last pipeline command is run in the main
process and can modify its variables, but what about the intermediate
ones?

a=1 | echo $a      -> prints "1" on zsh, prints nothing on ash,pdksh,bash

-- 
	Dan Nelson
	dnelson@allantgroup.com


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: local variable assignment and pipelines?
  2003-07-18 21:58 local variable assignment and pipelines? Dan Nelson
@ 2003-07-18 23:20 ` Philippe Troin
  2003-07-21  9:59 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Philippe Troin @ 2003-07-18 23:20 UTC (permalink / raw)
  To: Dan Nelson; +Cc: zsh-workers

Dan Nelson <dnelson@allantgroup.com> writes:

> a=1 | echo $a      -> prints "1" on zsh, prints nothing on ash,pdksh,bash

Even more fascinating:

% a=0; b=0; cat < /dev/null | a=1 | b=1 | cat; echo $a $b      
1 1                                         

Reassuring:

% a=0; b=0; cat < /dev/null | {a=1; while read i; do echo $i; done} | b=1 | cat; echo $a $b 
0 1

Phil.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: local variable assignment and pipelines?
  2003-07-18 21:58 local variable assignment and pipelines? Dan Nelson
  2003-07-18 23:20 ` Philippe Troin
@ 2003-07-21  9:59 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2003-07-21  9:59 UTC (permalink / raw)
  To: zsh-workers

Dan Nelson wrote:
> a=1 | echo $a      -> prints "1" on zsh, prints nothing on ash,pdksh,bash

It's an inevitable but unintended side effect of the way some
substitutions are done by the shell before it forks.  The list given in
subst.c is this one.

/* Do substitutions before fork. These are:
 *  - Process substitution: <(...), >(...), =(...)
 *  - Parameter substitution
 *  - Command substitution
 * Followed by
 *  - Quote removal
 *  - Brace expansion
 *  - Tilde and equals substitution
 */

My guess is this is left undefined by POSIX etc.  The stuff on pipelines
is fairly terse, but there might be something elsewhere since I didn't
look all that closely.

Where you would not want this to happen, and it doesn't, is if the
assignment immediately precedes a command.

pws


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-07-21  9:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-18 21:58 local variable assignment and pipelines? Dan Nelson
2003-07-18 23:20 ` Philippe Troin
2003-07-21  9:59 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).