zsh-users
 help / color / mirror / code / Atom feed
* Pipe input consumed where I didn't expect it to
@ 2016-12-04 21:13 Jesper Nygårds
  2016-12-05  2:12 ` Jun T.
  0 siblings, 1 reply; 2+ messages in thread
From: Jesper Nygårds @ 2016-12-04 21:13 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1050 bytes --]

I recently tried a command line of various pipes leading in to a shell
script that I've written, and it didn't behave as I expected it to do.

This reduced example demonstrates what I did. Suppose I have the following
script called streamtest:

#!/bin/zsh
if [[ ! -t 0 ]]; then
    while read x; do print "x $x"; done
else
    print "no pipe!"
fi

If I call it like this:
% for c in 1 2 3; do ./streamtest; done

I get the output:
no pipe!
no pipe!
no pipe!

No surprise there.

However, if I call it like this:
% for c in 1 2 3; do echo $c; done | while read f; do ./streamtest; done

I get the output:
x 2
x 3

Obviously my script consumes line 2 and 3, and prints them. Why is this the
case? I naively thought that the outer read (of f) on the command line
would be fed from the pipe, and then call "streamtest" without a pipe
leading into the script. In other words I thought I would get the output:

no pipe!
no pipe!
no pipe!

I assume this is just a misunderstand on my part, but if I wanted the
behaviour that I expected, how could I fix it?

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

* Re: Pipe input consumed where I didn't expect it to
  2016-12-04 21:13 Pipe input consumed where I didn't expect it to Jesper Nygårds
@ 2016-12-05  2:12 ` Jun T.
  0 siblings, 0 replies; 2+ messages in thread
From: Jun T. @ 2016-12-05  2:12 UTC (permalink / raw)
  To: zsh-users


On 2016/12/05, at 6:13, Jesper Nygårds <jesper.nygards@gmail.com> wrote:

> However, if I call it like this:
> % for c in 1 2 3; do echo $c; done | while read f; do ./streamtest; done
> 
> I get the output:
> x 2
> x 3
> 
> Obviously my script consumes line 2 and 3, and prints them. Why is this the
> case? I naively thought that the outer read (of f) on the command line
> would be fed from the pipe, and then call "streamtest" without a pipe
> leading into the script.

When the while loop starts, 'read f' is called once and consumes 1. Since
the read succeeds, now streamtest is called, which consumes rest of the
input (2 3) and exists. Now 'read f' is called again but fails, so the
loop ends.


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

end of thread, other threads:[~2016-12-05  2:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-04 21:13 Pipe input consumed where I didn't expect it to Jesper Nygårds
2016-12-05  2:12 ` Jun T.

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).