On Fri, Mar 14, 2003, shlomo solomon wrote about "Re: pipe question":
> So what's my problem? It seems that both the above solutions are writing to a 
> buffer and the actual screen output is not immediate, but in spurts. Let me 
> make that clearer. The original script creates a few hundred lines of output 
> that would normaly (if not for the pipe and GREP) scroll **gently** off the 
> screen during the few minutes it runs. But with both the above solutions, I 
> get nothing on screen at first. After a little while, about 100 lines scroll 
> at high speed and a while later, another 100, etc. This is certainly better 

This is happening because of a convention, an implementation detail, of the
standard C library. By convention, the "stdout" file pointer is line-buffered,
*unless* stdout is redirected not to a terminal (a pipe or a file) in which
case this file pointer becomes fully buffered, meaning that output is not
flushed by default until full buffers (e.g., 4096 characters) are accumulated.
The C library checks whether stdout is pointing to a terminal using isatty().

This is why
        grep ....
appears to output line by line while
        grep ... > file
writes to the file in chunks.

If you have access to the source-code of the program, you can add a
setlinebuf() call to make stdout line buffer, or add fflush() calls.
But since I assume you don't want to do that, you can use the "unbuffer"
utility on an existing program, e.g.,

        unbuffer grep .... > file

and it causes grep to think that its output is still a terminal, so it
remains line-buffered.

On my system (Redhat 8.0), "unbuffer" is implemented as a trivial (3-line)
"expect" stript; If you want an interesting exercise try writing a C
version using a different approach: set LD_PRELOAD to a shared library
containing a fake implementation of isatty(), one that always returns 1
(or returns 1 for fd 1 and calls the original function for the others).

-- 
Nadav Har'El                        |     Friday, Mar 14 2003, 10 Adar II 5763
[EMAIL PROTECTED]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |The meek shall inherit the Earth, for
http://nadav.harel.org.il           |they are too timid to refuse it.

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to