|
From: | John Kearney |
Subject: | Re: [Help-bash] Associative array questions |
Date: | Sat, 07 Jul 2012 22:18:08 +0200 |
User-agent: | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 |
Am 07.07.2012 21:54, schrieb Bill
Gradwohl:
#!/usr/bin/bashwhat you are thinking about is more like this func(){ IFS=$'\n'; eval vars=([black]=night [white]=snow $(cat /tmp/xxx) [yellow]=submarine [pink]=elephant) IFS=$' \t\n' declare -p vars } but don't do that just do someting like this func(){ vars=([black]=night [white]=snow [yellow]=submarine [pink]=elephant) while read -r i ; do vars[${i%%=*}]="${i#*=}" done declare -p vars } echo 'red=rose' >/tmp/xxx echo 'green=grass' >>/tmp/xxx echo 'blue=sky' >>/tmp/xxx 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.has to be set before parsing of the code to take effect. IFS=$'\n'; func(){ eval vars=([black]=night [white]=snow $(cat /tmp/xxx) [yellow]=submarine [pink]=elephant) declare -p vars } IFS=$' \t\n' the way you have set IFS it only effects expanded variables. basically its not really practical to use IFS in the way you are thinking. 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?no not quiet. See above
|
[Prev in Thread] | Current Thread | [Next in Thread] |