I create a pipe using
mkfifo /tmp/foo.pipe
Now, I want to try reading from the pipe for a maximum of 2 seconds, so I execute
read -t 2 line < /tmp/foo.pipe
The timeout does not occur. Read just sits there waiting for input from the pipe.
The manuals say that 'read' is supposed to work with named pipes. Does anyone have an idea why this is happening?
ls -al /tmp/foo.pipe
prw-r----- 1 foo bar 0 Jun 22 19:06 /tmp/foo.pipe
Your shell is blocking on the open() call before invoking the read builtin.
On Linux, you can open the FIFO for both read and write at the same time to prevent blocking on open; this is non-portable, but may do what you want.
read -t 2 <>/tmp/foo.pipe
Adapted from: Bash script with non-blocking read
If you just want to flush (and discard) the data from the FIFO:
dd iflag=nonblock if=/tmp/foo.pipe of=/dev/null &> /dev/null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With