bug-bash
[Top][All Lists]
Advanced

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

Re: mapfile usage


From: Stephane CHAZELAS
Subject: Re: mapfile usage
Date: Wed, 4 Feb 2009 19:59:30 +0000 (UTC)
User-agent: slrn/pre1.0.0-2 (Linux)

2009-02-4, 10:50(-08), Alex Reed:
> Can someone please explain how 'mapfile' should be used?  I am trying:
>
> cat file.txt | mapfile
> for i in ${MAPFILE};do echo $i; done
>
> and I see no output.

mapfile would be run in a subshell.

Try mapfile < file.txt

Note that the for loop syntax above is zsh's, not bash's.

In bash (and ksh from which it derives), you need:

for i in "${MAPFILE[@]}"; do echo "$i"; done


>I've tried adding the -t option to strip
> trailing newlines.  If I use the following command:
>
> mapfile -u file.txt
>
> I get the error:
>
> bash: mapfile: file.txt: invalid file descriptor specification
> Exit 1
[...]

The "help mapfile" is a bit confusing here:

help> mapfile: mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C 
callback] [-c quantum] [array]
help>     Read lines from a file into an array variable.
help> 
help>     Read lines from the standard input into the array variable ARRAY, or 
from

Looks like the first "Read lines..." was not intentional.


Now I wonder why a new builtin was created for that. It looks
like the same could have been done with very few lines of bash
code. Also the name is misleading at it suggests writing to the
array should write to the file (as the $mapfile associative
array in zsh), extending the "read" builtin for that would have
seemed more natural to me.

Also, it looks like it should guard against
seq 5 | mapfile -C echo  -c0

That command above cannot be interrupted with <Ctrl-C>

$ seq 5 | mapfile -C echo -c1
1
2
3
4

I would have expected 1,2,3,4,5 above from the "help mapfile"
description.

mapfile <&-

hangs as well (tight loop as well, cannot be interrupted).

-- 
Stéphane


reply via email to

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