Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15878
| Path | csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail |
|---|---|
| From | Léa Gris <lea.gris@noiraude.net> |
| Newsgroups | gnu.bash.bug |
| Subject | Lack of documentation about mapfile callback |
| Date | Sun, 9 Feb 2020 03:04:09 +0100 |
| Lines | 77 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.533.1581213907.2412.bug-bash@gnu.org> (permalink) |
| References | <ec577c4e-963c-26b0-94d2-0c8113e4c09c@noiraude.net> |
| NNTP-Posting-Host | lists.gnu.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | usenet.stanford.edu 1581213908 15642 209.51.188.17 (9 Feb 2020 02:05:08 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| To | bug-bash@gnu.org |
| Envelope-to | bug-bash@gnu.org |
| User-Agent | Telnet/1.0 [tlh] (PDP11/DEC) |
| Content-Language | en-US |
| X-detected-operating-system | by eggs.gnu.org: Genre and OS details not recognized. |
| X-Received-From | 2001:910:800::19 |
| X-BeenThere | bug-bash@gnu.org |
| X-Mailman-Version | 2.1.23 |
| Precedence | list |
| List-Id | Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org> |
| List-Unsubscribe | <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe> |
| List-Archive | <https://lists.gnu.org/archive/html/bug-bash> |
| List-Post | <mailto:bug-bash@gnu.org> |
| List-Help | <mailto:bug-bash-request@gnu.org?subject=help> |
| List-Subscribe | <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <ec577c4e-963c-26b0-94d2-0c8113e4c09c@noiraude.net> |
| Xref | csiph.com gnu.bash.bug:15878 |
Show key headers only | View raw
man bash.1
> When callback is evaluated, it is supplied the index of the next
> array element to be assigned and the line to be assigned to that
> element as additional arguments. callback is evaluated after the
> line is read but before the array element is assigned.
I can not find real-life implementation example of the mapfile callback
that fit the implied scenario of this behavior of invoking the callback
before the last array entry is assigned.
What I figured out by experimentation, is that while the last element is
not assigned to MAPFILE as seen from the callback context, the
assignment is effective after the callback returns.
Example:
----- BEGIN BASH
#!/usr/bin/env bash
callback() {
echo "Entering Callback"
printf 'Next index is: %d\n' $1
printf 'Next entry is: %q\n' "$2"
printf 'MAPFILE size: %d\n' "${#MAPFILE[@]}"
typeset -p MAPFILE
echo "Exiting Callback"
}
mapfile -t -C callback -c 3 <<'EOF'
Entry0
Entry1
Entry2
Entry3
Entry4
Entry5
Entry6
Entry7
Entry8
EOF
----- END BASH
And then the output:
----- BEGIN OUTPUT
Entering Callback
Next index is: 2
Next entry is: Entry2
MAPFILE size: 2
declare -a MAPFILE=([0]="Entry0" [1]="Entry1")
Exiting Callback
Entering Callback
Next index is: 5
Next entry is: Entry5
MAPFILE size: 5
declare -a MAPFILE=([0]="Entry0" [1]="Entry1" [2]="Entry2" [3]="Entry3"
[4]="Entry4")
Exiting Callback
Entering Callback
Next index is: 8
Next entry is: Entry8
MAPFILE size: 8
declare -a MAPFILE=([0]="Entry0" [1]="Entry1" [2]="Entry2" [3]="Entry3"
[4]="Entry4" [5]="Entry5" [6]="Entry6" [7]="Entry7")
Exiting Callback
----- END OUTPUT
It reveals the weirdness of running the callback before the last
assignment from the quantum.
First call to callback has a MAPFILE with 2 entries, while the next two
have 3 entries.
There must be a reason or an intended scenario for this implementation,
but with so few documentation and no real-world usage example, it is
unclear to me.
--
Lea Gris
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Lack of documentation about mapfile callback Léa Gris <lea.gris@noiraude.net> - 2020-02-09 03:04 +0100
csiph-web