tags:

views:

196

answers:

2

Is it possible to do selects or polls on file descriptors in bash? The essence of what I'm trying to do is this:

mkfifo fifo
exec 3<fifo
PROMPT_COMMAND="while read -t 0 line; do echo \$line; done <&3"

The exec is there to keep the pipe open (otherwise it would be closed at the end of each loop). In theory, this would output anything entering the pipe before each prompt. However, it doesn't seem to work, as with -t0 it doesn't even try to read.

-t 1 works like a charm, but that forces a one second delay at every prompt, which is not what I want.

Optimal would be to do a select with .2 seconds timeout (if i'm executing a command which in turn causes something to be written to the pipe, there's bound to be a short delay as I'm working with asynchronous messages), and that delay I can live with. Zero timeout would probably be ok, then I'll just create a program to have a subsecond delay.

Any ideas?

A: 

Wouldn't the following be equivalent?

$> tail -f fifo

You can also specify sleep intervals ('-s'), retries and --max-unchanged-stats, etc.

Jeach
tail no (tail is for seekable files), cat would do what you're suggesting, but it'd bust your command line if data arrives when you're typing your command. Or you'd have to do it in a different console if you have one.
roe
+1  A: 

I stumbled on this today, and it actually solves my problem quite elegantly. screen allows the current terminal window to be split, where I can reduce one part of the window to just a couple of lines, where I keep my input, and just cat fifo in the other part of the window.

Screen definitely rocks a lot more than I already knew (detach alone makes it one of the best tools ever).

roe