Hi all,
I have a script with the following gist:
declare -a index=(0 1 2 3 4);
declare -a animals=(dog cat horse penguin cow);
declare -a fruits=(orange apple grapes peach mango);
declare -a drinks=(juice milk coffee tea coke);
declare -a cities=(toronto paris london glasgow sydney);
declare -a countries=(canada france england scotland australia);
declare -a all=(animals fruits drinks cities countries);
for i in "${index[@]}" ; do
echo ${i};
animals="${animals["${i}"]}";
echo $animals;
fruits="${fruits["${i}"]}";
echo $fruits;
drinks="${drinks["${i}"]}";
echo $drinks;
cities="${cities["${i}"]}";
echo $cities;
countries="${countries["${i}"]}";
echo $countries;
done
And this is the output:
0
dog
orange
juice
toronto
canada
1
cat
apple
milk
paris
france
2
horse
grapes
coffee
london
england
3
penguin
peach
tea
glasgow
scotland
4
cow
mango
coke
sydney
australia
Because my actual script has more arrays to loop through and I'll be adding more as time goes on, I came up with an 'all' array that has the array names in it. Then I attempted to loop through each array as such:
for i in "${index[@]}" ; do
echo ${i};
for j in "${all[@]}" ; do
echo ${!j[${i}]};
done
done
But this time I'm only getting items at index 0, as per the output below:
0
dog
orange
juice
toronto
canada
1
2
3
4
abc@xyz$
I've been struggling with this one for a while now, and I'm not entirely sure what I'm trying to accomplish is possible with Bash. Any help is appreciated!
Thanks!