Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Pipe input consumed where I didn't expect it to



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?


Messages sorted by: Reverse Date, Date, Thread, Author