public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* BASH 4.4 mapfile/readarray/read builtins mis-behaving with pipe
@ 2018-07-17 12:57 BloomingAzaleas
  2018-07-17 12:57 ` Steven Penny
  2018-07-18 11:17 ` BASH 4.4 mapfile/readarray/read builtins mis-behaving with pipe [edit] documentation bug BloomingAzaleas
  0 siblings, 2 replies; 7+ messages in thread
From: BloomingAzaleas @ 2018-07-17 12:57 UTC (permalink / raw)
  To: cygwin

Windows 10 Pro 10.0.17134 N/A Build 17134 patched through 15 July 
2018
Cygwin 2.10.0(0.325/5/3)
Bash 4.4.12(3)

Cygwin man pages show:

   mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u 
fd]  [-C  callback]  [-c quantum] [array]
   readarray  [-d delim] [-n count] [-O origin] [-s count] [-t] 
[-u fd] [-C callback] [-c        quantum] [array]
     Read lines from the standard input into the indexed array 
variable  array,  or ...

  read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N 
nchars]  [-p  prompt]  [-t timeout] [-u fd] [name ...]
      One  line  is read from the standard input, or ...

So the expectation is that all bash stdin re-directions such as 
pipes and left chevs should work.  For mapfile/readarry, the 
output array variable MAPFILE is described as being created if 
not array_var argument is given to mapfile.

A) Searched cygwin email list with terms 'mapfile', 'readarray' 
and 'read builtin'.  No obvious hits of recent vintage in summary 
result list.

B) Confirm, not using mapfile/readarry/read, that bash stdin 
redirs work as expected.

echo multi-line_arg | /bin/cat

/bin/cat < some_unix_fmt_file

/bin/cat <<END
some
lines
END

all work as expected.

C) Test mapfile/readarray/read in various stdin redir situations. 
Basic harnesses are :

mapfile/readarry HARNESS:
stdin or fd 0 feed to mapfile
then
: "${MAPFILE[@]:?MAPFILE null or unset}"
if get past the ${:?} test then dump MAPFILE with a simple /for 
/loop as: for ent in "${MAPFILE[@]}" ; do echo ="${ent}"= ; done

read HARNESS:
stdin feed to 'read foo'
then
: "${foo:?foo null or unset}"
if get past the ${:?} test then dump variable foo with echo 
="${foo}"=

CASES:
echo multi-line_arg | mapfile
cat multi-line_unix_fmt_file | mapfile
RESULT:
Fail with MAPFILE null or unset
If 'for' loop forced, "${MAPFILE[@]}" expands to zero words.

CASE:
echo multi-line_arg | read foo
cat multi-line_unix_fmt_file | read foo
  RESULT:
Fail with variable foo null or unset

CASES: left chev re-directions
mapfile < some_unix_fmt_file
mapfile <<END multi-line_here_doc
read < some_unix_fmt_file
read <<END multi-line_here_doc
RESULT:
Work as expected

CASES: mapfile or read in a sub-shell
echo multi-line_arg | ( mapfile ; stuff )
echo multi-line_arg | ( read foo; stuff )
RESULT:
Work as expected.  However, since mapfile/read result vars are 
not accessible in parent shell context, can force a notable 
re-structuring of a script to use sub-shells.

CONCLUSIONS:
For bash builtins mapfile/readarray and read, stdin pipe 
re-direction directly to the builtin fails as if the builtin 
read()/fread() sees a silent pre-mature EOF on the pipe.  In the 
sub-shell tests, the pipe re-direction happens for the sub-shell 
as a whole before commands internal to the sub-shell get 
processed for individual re-dirs, so no re-dir processing 
directly for the builtin.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-07-22  5:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-17 12:57 BASH 4.4 mapfile/readarray/read builtins mis-behaving with pipe BloomingAzaleas
2018-07-17 12:57 ` Steven Penny
2018-07-18 11:17 ` BASH 4.4 mapfile/readarray/read builtins mis-behaving with pipe [edit] documentation bug BloomingAzaleas
2018-07-21  1:19   ` Eric Blake
2018-07-22  5:42     ` BloomingAzaleas
2018-07-23  2:15       ` Steven Penny
2018-07-23  6:32         ` Brian Inglis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).