Newsgroups: comp.unix.shell Date: Fri, 12 Dec 2003 04:54:47 GMT
> Is there a way (in shell) to set the stdout as unbuffered? > > I want to catch my connection speed from the wvdial output, while > still be able to see it. > > wvdial | tee /tmp/wvdial.log > > would normally do. However, the problem is that the > /tmp/wvdial.log is empty, even though the wvdial has already made > some output. > > I've been looking around in my local man pages, but didn't find the > solution. I tried stty raw, but that didn't help.
The `problem' is the standard I/O library. When it is writing to a terminal it is unbuffered, but if it is writing to a pipe then it sets up buffering.
So you have 2 choices.
As you are talking about a program that you have the source code to, you can tell the stdio library not to buffer the output.
Add a line like
setvbuf(stdout,NULL, _IOLBF,BUFSIZE);
near the start of the program. Or,
an answer closer to the spirit of your question, make the stdio library think it is writing to a terminal. There are a number of programs around which can do this, the most famous is 'expect', and it even comes with an example program which does this. On my system it is installed as
/usr/share/doc/expect/examples/unbuffer
and its contents are
#!/bin/sh # \ exec expect -- "$0" ${1+"$@"} # Description: unbuffer stdout of a program # Author: Don Libes, NIST
set stty_init "-opost" eval spawn -noecho $argv set timeout -1 expect
There is also Dan Bernsteins 'pty' program.
Icarus Sparry
> > Is there a way (in shell) to set the stdout as unbuffered? > > 2) an answer closer to the spirit of your question, make the stdio library > think it is writing to a terminal. > > There is also Dan Bernsteins 'pty' program.
A pty is described in Richard Stevens' book "Advanced Programming in the Unix Environment".
The source code is available at http://www.kohala.com/start/apue.html
Janis