Originally published January 29, 2019 @ 12:58 am
Bash does not support multi-dimensional arrays, but there is a way to imitate this functionality, if you absolutely have to.
As a quick example, here’s a data table representing a two-dimensional array.
1 | 2 | 3 | |
a1 | n1 | n2 | n3 |
a2 | m1 | m2 | m3 |
And here’s the graphical representation of this two-dimensional array with the values you would expect for each y[x] position:
a2[0] | a2[1] | a2[2] | |
a1[0] | n1m1 | n1m2 | n1m3 |
a1[1] | n2m1 | n2m2 | n2m3 |
a1[2] | n3m1 | n3m2 | n3m3 |
And here’s how we do it:
a=('a1=(n1 n2 n3)' 'a2=(m1 m2 m3)') for i in "${a[@]}"; do eval "${i}"; done x=0;y=2;echo "${a1[${x}]}${a2[${y}]}"
What about a three-dimensional array? Not gonna draw you a cubical table, but here’s the code:
a=('a1=(n1 n2 n3)' 'a2=(m1 m2 m3)' 'a3=(o1 o2 o3)') for i in "${a[@]}"; do eval "${i}"; done x=0;y=2;z=1;echo "${a1[${x}]}${a2[${y}]}${a3[${z}]}"
This may seem a bit awkward and laborious, compared to the proper programming languages, but this can be extremely useful.
Think about it: a three-dimensional array holding data like timestamps, CPU I/O wait time, and network bandwidth utilization. This would be perfect for analyzing a CPU bottleneck that you suspect has something to do with time of day and network activity.
Experienced Unix/Linux System Administrator with 20-year background in Systems Analysis, Problem Resolution and Engineering Application Support in a large distributed Unix and Windows server environment. Strong problem determination skills. Good knowledge of networking, remote diagnostic techniques, firewalls and network security. Extensive experience with engineering application and database servers, high-availability systems, high-performance computing clusters, and process automation.