bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading bytes one-by-one from pipe of process substitution


From: stephane_chazelas
Subject: Re: Reading bytes one-by-one from pipe of process substitution
Date: Thu, 28 Sep 2006 18:16:54 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

On Thu, Sep 28, 2006 at 09:12:47PM +0600, root@umc.ustu.ru wrote:
[...]
> Description:
>         When Process substitution is used, BASH
>         reads from such descriptor by one bytes!!

Of course it does. A shell is a shell, it's a command that runs
other commands.

read var

Reads one line of input (potentially more without -r). You don't
want it to read more than one line of input, because you want
the next command to be able to read the next line.

And the only way to make sure that only one line is read and not
more is to read one character at a time until the LF character
is found.

For regular files, shells like bash or ksh do some
optimisations, they read a buffer worth of data, and then, in
case another command is to be run, they seek back (move the
"read" cursor position if you prefer) to the character after the
LF. This kind of thing can't be done with a pipe of course, you
can't tell the process at the other end "please restart from
that newline character". It can only be done with regular files
(not with devices, sockets, pipes...).

So, it's a perfectly normal behavior. That's rather the
optimisation that is not normal (in the sense that it reads more
than what you may expect it to).

> Repeat-By:
>         --------------------------
>         /tmp/test:
>       --begin--
>         #!/bin/bash
>         while read line; do
>         qwe=qwe
>       done < <( cat /etc/passwd )
>       --end--
>       chmod +x /tmp/test
>       strace /tmp/test
>       --------------------------------------
>       You'll see that BASH read bytes from pipe one-by-one.
>       Another script will not trigger this problem:
>         #!/bin/bash
>       while read line; do
>         qwe=qwe
>       done < /etc/passwd
[...]

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]