How to do 'Loop' in multidimensional 'Array' in Bash?

1

Situation:

I need to create a multidimensional array script. Example:

Table 1 > > Fields id and name

Table 2 > > id and phone fields

Current script:

#!/bin/bash

declare -A arr
arr[tabela1]=id
arr[tabela2]=id

for i in "${!arr[@]}"
do
        echo "Tabela: $i"
        echo "Campo: ${arr[$i]}"
done

return

Problem:

How to make a multidimensional array and a loop to insert, in addition to the 'id' according to the script above, several columns; example, 'name', 'phone', etc., as demonstrated in Situation ?

Thank you!

    
asked by anonymous 08.09.2015 / 16:26

1 answer

1

As far as I understand, you want an array that stores id and name information. If it is ...

Creating array in bash is simpler than you think. Example:

#!/bin/bash
array_( "nome1" "nome2" "nome3" )
#technique 1 for print all elements
echo "${array_[@]:0}"
#technique 2 for print all elements
for ((id=0; id<{#$array_[@]}; id++)); do
  echo "Bands: ${array_[$id]}"
done

ps: In bash, the use of ${#array[@]} is the same as saying: - " array , how many elements are stored in you?"

Using the above example to print the name , something like this can be done:

#!/bin/bash
bands=("slayer" "sodom" "megadeth")
for ((i=0; i<${#bands[@]}; i++)); do
  echo "Bands: ${bands[$i]}"
done

output:

Bands: slayer
Bands: sodom
Bands: megadeth

In theory we know that we will have to work with two arrays and print them in parallel, so I did this (pseudo-) code:

** Even though it is not recommended to use eval , but beauty ... this is just an example! ;)

Using the code below demonstrates a 'technique' to print just the id .

#!/bin/bash
#uncomment to enable debug
#set -x
main() {
local idx_tab1=$(set -- {1..5} && echo ${@})
local idy_tab2=$(set -- {1..5} && echo ${@})

display=('x1=${idx_tab1[@]}' 'y1=${idy_tab2[@]}')

for id in "${display[@]}"; do
  eval ${id[@]}
done
  echo "idx_tab1: ${x1[@]//\ /$'\n'idx_tab1: }"
  echo "" # skip one line --//--
  echo "idy_tab2: ${y1[@]//\ /$'\n'tdy_tab2: }"
}

main ${@:1}

output:

idx_tab1: 1
idx_tab1: 2
idx_tab1: 3
idx_tab1: 4
idx_tab1: 5 

idy_tab2: 1
tdy_tab2: 2
tdy_tab2: 3
tdy_tab2: 4
tdy_tab2: 5 

Putting all together

Example 1: Manual, without loop.

source

#!/bin/bash
#uncomment to enable debug
#set -x
main() {
#array "name" for get "id"
local table1=("t1_nome1" "t1_nome2" "t1_nome3")
local table2=("t2_nome1" "t2_nome2" "t2_nome3")

echo "Table: 1"
echo "Column: ${table1[@]:0}"
echo $'\n'
echo "Table: 2"
echo "Column: ${table2[@]:0}"
}

main ${@:1}

output

Table: 1
Column: t1_nome1 t1_nome2 t1_nome3


Table: 2
Column: t2_nome1 t2_nome2 t2_nome3

Example 2, with loop while :

#!/bin/bash
#uncomment to enable debug
#set -x
main() {
#array "name" for get "id"
local table1=("t1_nome1" "t1_nome2" "t1_nome3")
local table2=("t2_nome1" "t2_nome2" "t2_nome3")
local len_t=("table1" "table2")
local id=1

while [[ "$id" -lt "${#len_t[@]}"  ]];
do
  echo "Table: [$id]"
  echo "Column: ${table1[@]:0}"
  let "id = $id + 1"
  echo $'\n'
  echo "Table: [$id]"
  echo "Column: ${table2[@]:0}"
done
}

main ${@:1}

output:

Table: [1]
Column: t1_nome1 t1_nome2 t1_nome3


Table: [2]
Column: t2_nome1 t2_nome2 t2_nome3

Anyway, to get the id of the tables, I have to append them to the array so I can get the exact size and loop counting from 1 (not from scratch! ) to the maximum size of the array. This is what I did using while : [[ "$id" -lt "${#array[@]}" ]] .

Well ... I think that's what you need! :)

    
23.01.2017 / 19:30