jeudi 29 janvier 2015

Loop over associative arrays by substring


In the following code, I create some associative arrays in a loop. The consist of two strings, a string identifier and a year. After creation, I want to access the arrays in a loop based on the identifier only.



#!/bin/bash

# Declare associative arrays of journal-year combinations
A_JOURNAL_LIST={JF,JFE,RFS}
B_JOURNAL_LIST={JBF,JFI,JMCB}
ALL_JOURNAL_LIST={JF,JFE,RFS,JBF,JFI,JMCB}
for year in {1998..2000} {2009..2011}
do
eval "A_$year=($A_JOURNAL_LIST-$year) ;"
eval "B_$year=($B_JOURNAL_LIST-$year) ;"
eval "all_$year=($ALL_JOURNAL_LIST-$year) ;"
done

for journal in A B all
do
echo "${'$journal'_1999[@]}"
done


I got a bad substitution error all the time I try a lot of combinations. What's wrong?



Aucun commentaire:

Enregistrer un commentaire