Re: AW: Questions to bash "read" builtin functionality

2012-12-19 Thread Chet Ramey
On 12/17/12 3:34 AM, Fiedler Roman wrote:

> From your description, I would expect 
> 
> --- HYPOTHETICAL-OUTPUT---
> # ./FragmentedSend.py | ./BashReadTest 
> Read status 142, value ""
> Read status 142, value "Stat"  << the partial read
> FAILED READ: "us: OK"
> Traceback (most recent call last):
>   File "./FragmentedSend.py", line 16, in 
> os.write(1, nextSendData[0:sendLength])
> OSError: [Errno 32] Broken pipe
> --- HYPOTHETICAL-OUTPUT---
> 
> Is this consistent with what you would be expecting!

I get the following with a few runs of the above code against a freshly-
built bash-4.2.39 (x24 is just a script that wraps the pipeline):

$ ./bash ./x24
Read status 142, value ""
Read status 142, value "Status:"
Read status 142, value ""
FAILED READ: "OK"
Traceback (most recent call last):
  File "./FragmentedSend.py", line 16, in 
os.write(1, nextSendData[0:sendLength])
OSError: [Errno 32] Broken pipe
$ ./bash ./x24
Read status 142, value ""
Read status 142, value "Statu"
Read status 142, value "s:"
FAILED READ: "OK"
Traceback (most recent call last):
  File "./FragmentedSend.py", line 16, in 
os.write(1, nextSendData[0:sendLength])
OSError: [Errno 32] Broken pipe
$ ./bash ./x24
Read status 142, value ""
Read status 142, value "Status: OK"
FAILED READ: ""
Traceback (most recent call last):
  File "./FragmentedSend.py", line 16, in 
os.write(1, nextSendData[0:sendLength])
OSError: [Errno 32] Broken pipe
$ ./bash ./x24
Read status 142, value "Sta"
Read status 142, value "tus: O"
FAILED READ: "K"
Traceback (most recent call last):
  File "./FragmentedSend.py", line 16, in 
os.write(1, nextSendData[0:sendLength])
OSError: [Errno 32] Broken pipe

I get the same results if I run the pipeline in an interactive shell.
This is pretty much what I expect.  The whitespace and newlines will
disappear due to read's $IFS processing if they're first or last in
the string.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: AW: Questions to bash "read" builtin functionality

2012-12-19 Thread Greg Wooledge
On Wed, Dec 19, 2012 at 04:49:32PM -0500, Chet Ramey wrote:
> I get the same results if I run the pipeline in an interactive shell.
> This is pretty much what I expect.  The whitespace and newlines will
> disappear due to read's $IFS processing if they're first or last in
> the string.

Which is why you typically want:

  IFS= read -r foo

This is especially true when you're reading N bytes at a time and need
them preserved verbatim.  (You can also use -d '' to suppress stopping
on a newline character, if that helps.)