[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Built-in printf Sits Awkwardly with UDP.

From: Steven W. Orr
Subject: Re: Built-in printf Sits Awkwardly with UDP.
Date: Mon, 18 Jul 2011 10:46:19 -0400
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0

On 7/18/2011 10:14 AM, Ralph Corderoy wrote:

(I'm not subscribed to the list so please keep me CC'd.)


     printf 'foo bar\n'>/dev/udp/localhost/4242

with bash's printf built-in, it works fine.  One packet is sent.
However, make that two lines

     printf 'foo\nbar\n'>/dev/udp/localhost/4242

and two packets are sent, one per line.  If the aim is to send a string
of data in one packet this causes problems.

Using /usr/bin/printf from coreutils gives one write(2), so that's a
possible workaround.

Is this happening because the built-in printf is using putchar(3) in the
PC() macro and stdio thinks file descriptor 1 is still to a tty so it's
persisting in line buffering?  It would seem nicer if fewer write(2)s
were done when stdout isn't a tty, and not just for UDP use.

Cheers, Ralph.

Ooh! Can I try to answer this one?!

Bash is always running in unbuffered mode, so every time a line is written, it automatically gets flushed. So... The only way to write in fully buffered mode with control over when things get flushed is to do it from a program that is not a bash script. So, yes. You need to do something outside of bash to get your output to the device.

Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

reply via email to

[Prev in Thread] Current Thread [Next in Thread]