[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Associative array questions
From: |
Chet Ramey |
Subject: |
Re: [Help-bash] Associative array questions |
Date: |
Sun, 08 Jul 2012 13:05:35 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 |
On 7/7/12 3:54 PM, Bill Gradwohl wrote:
> produces:
>
> /tmp/yyy: line 7: vars: [red]=rose: must use subscript when assigning
> associative array
> /tmp/yyy: line 7: vars: [green]=grass: must use subscript when assigning
> associative array
> /tmp/yyy: line 7: vars: [blue]=sky: must use subscript when assigning
> associative array
> declare -A vars='([yellow]="submarine" [white]="snow" [black]="night"
> [pink]="elephant" )'
>
> /tmp/yyy: line 7: vars: [red]=rose: must use subscript when assigning
> associative array
> /tmp/yyy: line 7: vars: [green]=grass: must use subscript when assigning
> associative array
> /tmp/yyy: line 7: vars: [blue]=sky: must use subscript when assigning
> associative array
> declare -A vars='([yellow]="submarine" [white]="snow" [black]="night"
> [pink]="elephant" )'
>
> 1) Why does the cat not work? What is it REALLY complaining about, as the
> individual lines are formatted properly.
The compound assignment is complaining because the word it sees --
$(cat /tmp/xxx) -- is not formatted correctly as an assignment statement.
The error message in bash-4.2 is somewhat misleading; the current version
gives a message like:
./x1: line 7: vars: $(cat /tmp/xxx): must use subscript when assigning
associative array
> 2) Since IFS is set to just the newline character, I was surprised to see
> that [black] does not equal 'night [white]=snow' . It is still breaking
> things up on the space.
IFS is only used to split the results of expansion. The words in a
compound assignment are run through the shell parser and split on shell
metacharacters.
> 3) In the second execution of func, the last line of the file does not
> contain a newline and yet its able to locate the [yellow]= and isolate it
> from whatever it doesn't like on the last line of the cat. The IFS appears
> to be ignored in locating the keys. Are keys located first and then
> whatever is between keys is the value?
Not relevant to the question.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/