bug-bash
[Top][All Lists]
Advanced

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

Re: read -t 0 fails to detect input.


From: Chet Ramey
Subject: Re: read -t 0 fails to detect input.
Date: Thu, 19 Dec 2019 11:40:00 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.3.0

On 12/18/19 6:40 PM, Bize Ma wrote:
It seems that read -t 0 should detect if there is input from a pipe (and
others).

 From man bash:

If  timeout is 0, read returns immediately, without trying to read any
data.
The exit status is 0 if input is available on the specified file
  descriptor, non-zero otherwise.

Bash-5.0 uses select/FIONREAD to determine whether or not there is input
available on the file descriptor. Those don't wait; they test whether or
not there is input on the specified file descriptor at the point they are
called.


So, it seems that this should print 1:

$ true | read -t 0 var; echo $?
1

And this should print 0 (input available), but it doesn't (most of the
time).

$ echo value | read -t 0 var ; echo $?
1

This is inherently racy: there is no guarantee that the echo will have
written anything to the pipe by the time the read runs to test it. It
depends on how the system schedules the processes.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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