Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

where did output redirection go?

223 views
Skip to first unread message

Tomasz Chmielewski

unread,
Mar 7, 2008, 4:32:10 AM3/7/08
to
Consider a following shell code executed in bash:

$ while true; do date; sleep 1; done | awk '{print $4}'
10:23:19
10:23:20
10:23:21
10:23:22
10:23:23
[ctrl+c]

It prints something to the standard output.


Now, let's use the same commands, but instead, redirect the output to a
file:

$ while true; do date; sleep 1; done | awk '{print $4}' > out.txt

[wait a minute here, and press ctrl+c]


"out.txt" file is empty, nothing was printed to the screen. Where did
the output redirection go?


--
Tomasz Chmielewski
http://wpkg.org

Janis

unread,
Mar 7, 2008, 5:36:03 AM3/7/08
to
<off-topic>

On 7 Mrz., 10:32, Tomasz Chmielewski <t...@nospam.syneticon.net>
wrote:

In an I/O buffer that hasn't got filled enough to be flushed
before you aborted the process.

To understand, try...

while true; do date; sleep 1; done | head -3 | awk '{print $4}' >
out.txt

(BTW, this is off-topic here; comp.unix.shell is more appropriate.)

Janis

>
> --
> Tomasz Chmielewskihttp://wpkg.org

Tomasz Chmielewski

unread,
Mar 7, 2008, 5:58:36 AM3/7/08
to
Janis schrieb:

>> "out.txt" file is empty, nothing was printed to the screen. Where did
>> the output redirection go?
>
> In an I/O buffer that hasn't got filled enough to be flushed
> before you aborted the process.
>
> To understand, try...
>
> while true; do date; sleep 1; done | head -3 | awk '{print $4}' >
> out.txt
>
> (BTW, this is off-topic here; comp.unix.shell is more appropriate.)

Actually, it's awk, not shell setting (or any other program that
buffers, like grep).

I solved it in gawk by adding {fflush()}:

iostat dm-1 -d 1 -k | awk '/dm-1/{print $3" "$4}{fflush()}' >output.st


For grep, BTW, the option is "--line-buffered".

Janis

unread,
Mar 7, 2008, 7:39:16 AM3/7/08
to
On 7 Mrz., 11:58, Tomasz Chmielewski <t...@nospam.syneticon.net>
wrote:

> Janis schrieb:
>
> >> "out.txt" file is empty, nothing was printed to the screen. Where did
> >> the output redirection go?
>
> > In an I/O buffer that hasn't got filled enough to be flushed
> > before you aborted the process.
>
> > To understand, try...
>
> > while true; do date; sleep 1; done | head -3 | awk '{print $4}' >
> > out.txt
>
> > (BTW, this is off-topic here; comp.unix.shell is more appropriate.)
>
> Actually, it's awk, not shell setting (or any other program that
> buffers, like grep).

No, it's neither awk nor grep that buffers; it's the OS (Unix in
your example).

>
> I solved it in gawk by adding {fflush()}:
>
> iostat dm-1 -d 1 -k | awk '/dm-1/{print $3" "$4}{fflush()}' >output.st
>
> For grep, BTW, the option is "--line-buffered".
>
> --
> Tomasz Chmielewskihttp://wpkg.org

Mind, the buffering is an OS issue, all the pipelines and external
commands is a unix shell issue, and the option you presented is an
tool specific issue (not even standardized), and the only awk'ish
part of the posting is either trivial (print $4) or non-standard
(fflush() seems not be defined by POSIX, according to the GNU docs).

Anyway; glad you've solved the issue once you knew about buffering.

Janis

Andrew Schorr

unread,
Mar 9, 2008, 10:26:02 PM3/9/08
to
On Mar 7, 8:39 am, Janis <janis_papanag...@hotmail.com> wrote:
> Mind, the buffering is an OS issue, all the pipelines and external
> commands is a unix shell issue, and the option you presented is an
> tool specific issue (not even standardized), and the only awk'ish
> part of the posting is either trivial (print $4) or non-standard
> (fflush() seems not be defined by POSIX, according to the GNU docs).

Actually, this is not an O/S issue, it's a C stdio library
issue. The gawk fflush call instructs the stdio library to
write out the data to the operating system kernel. Once the kernel
has it, there's no more risk of losing the data.

Regards,
Andy

0 new messages