in reply to Re^4: how can I redirect the output of a program and get the exit code too?
in thread how can I redirect the output of a program and get the exit code too?
That probably depends on the external program you are calling.
Libc normally sets up a stream buffered, more precisely line buffered if it's connected to a terminal, full buffered otherwise. This means that unless the program you call will do explicitly otherwise, you'll get the output only after each newline if it's connected to a terminal, but the newline does not flush the buffer if it's connected to a pipe as in your case, so you'll get the output chunk by chunk. Some programs (like cat) are intelligent enought to smartly override this, but some programs just leave it as is (the most annoying one being gnu tr).
If the program you call is a perl script you have written, you can edit it to flush the output whenever you need it to appear (with use IO "Hanlder"; flush STDOUT; on newer perls). If it's a C program you wrote, you can flush the output with fflush(stdout), or force line buffering with setlinebuf(stdout); (I'm not sure in this one). If it's a program you cannot change, you probably can't do many things, except maybe to call if through a virtual terminal, which I don't know how to do from perl, and can have other effects.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: how can I redirect the output of a program and get the exit code too?
by vikee (Sexton) on Jun 23, 2004 at 11:24 UTC | |
by ambrus (Abbot) on Jun 23, 2004 at 11:57 UTC |