Bash, transforming a string list of words in a array

0

Sorry if I post a question on a topic discussed dozens of times. I'm trying to work on an element of an associative array and assign it to an array. I know that in bash associative arrays are monodimendional, but by using the string expansion of the quotes in bash I would assign an array. In the end I managed to do it in two steps but I don't know why I need that extra assignment and if I can avoid it.

Here's the script

FILE="/home/user/scripts/snapstbylist"

 declare -A stby
 while read line; do
    DS=${line%@*}
    SNAPSHOT=${line#*@}
    stby+=(["$DS"]=" $SNAPSHOT")
done < <(awk '/stby/{print $1}' $FILE)

for ds in "${!stby[@]}"; do
 #       echo ":-) $ds"
         DATASET2=( "${stby["$ds"]}" )

         DATASET=($DATASET2)

         LAST="${DATASET[-1]}"
         echo "The dataset list $ds contains ${#DATASET[@]} elements, i.e. ${DATASET[@]}"
done

DATASET2 is a string with words in it, eventually DATASET is the array I want. Is there a way to avoid the assignment DATASET=($DATASET2) and have as DATASET2 the array I want?

Extract of the file /home/user/scripts/snapstbylist

zserver02/home/GROUP/biastby@d20140123  227K    -       471M    -
zserver02/home/GROUP/biastby@d20140128  233K    -       471M    -
zserver02/home/GROUP/biastby@d20140129  206K    -       471M    -
zserver02/home/GROUP/biastby@d20140130  240K    -       471M    -
zserver02/home/GROUP/biastby@d20140131  438K    -       471M    -
zserver02/home/GROUP/biastby@d20140201  244K    -       471M    -
zserver02/home/GROUP/biastby@d20140202  220K    -       471M    -
zserver02/home/GROUP/biastby@d20140204  0       -       832M    -
zserver02/home/GROUP/bloombergstby@d20131119    0       -       148K    -
zserver02/home/GROUP/bloombergstby@initback02   0       -       148K    -
zserver02/home/GROUP/caestby@d20131228  0       -       626M    -
zserver02/home/GROUP/gtistby@d20140125  0       -       131K    -
zserver02/home/GROUP/juastby@d20140107  0       -       130K    -
zserver02/home/GROUP/kepstby@d20140118  0       -       138K    -
zserver02/home/GROUP/koastby@d20131227  93K     -       145K    -

I could sum in this way, why

"${stby["$ds"]}"

in

DATASET2=( "${stby["$ds"]}" )

is not expanded?

Thanks in advance,

Alex

user2984629

Posted 2014-02-27T12:21:01.247

Reputation: 23

1imho, bash has very restrictive and limited features concerning arrays. why not using a little more complete language like python/perl ? – mveroone – 2014-02-27T13:12:17.230

@Kwaio, as soon as I complete this project I'll do, for the moment other scripts, part of the same project are in bash and hence I have to stick with it :) – user2984629 – 2014-02-27T13:15:55.653

No answers