mapfile usage
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. 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 This is sad... Any suggestions?
Re: Migrating from tcsh to bash (issues)
2009-02-3, 22:07(+00), Simos: > On Tue, Feb 3, 2009 at 9:24 PM, Stephane CHAZELAS > wrote: >> 2009-02-3, 19:38(+00), Simos: >> [...] >>> I have been using tcsh for a long time and I plan to move to bash. >> [...] >> >> I'd recommend moving to zsh instead. The transition is easier >> from tcsh and this way to won't have to move from bash to zsh >> later on. > Oops! I hadn't realised we were on the bash newsgroup/ML. Sorry for the troll. > I do not know how well zsh is positioned to become default shell in distros. Well, I wouldn't say that's the right question to ask. It's like saying "I do not know how well Linux is positioned to become default OS on PCs". > My goal is towards enhancing the default shell found in mainstream > distributions. > This helps in training new users, and getting bigger adoption of FLOSS > to the masses. > For example, see https://wiki.ubuntu.com/Spec/EnhancedBash > for the spec to change the default shell settings. It's an amazing > usability opportunity and task. And a really difficult one as well. [...] -- Stéphane
Re: Migrating from tcsh to bash (issues)
Stephane CHAZELAS wrote: > > Oops! I hadn't realised we were on the bash newsgroup/ML. Sorry > for the troll. It's ok. We're a friendly bunch. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: mapfile usage
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 "${mapfi...@]}"; 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 $ 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
Re: mapfile usage
On Feb 4, 2:59 pm, Stephane CHAZELAS wrote: > 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 "${mapfi...@]}"; 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 > > $ 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 Nice. Thank you!