How to 'Loop' in Multi-dimensional Array in Bash?

Asked

Viewed 403 times

1

Situation:

I need to create a multi-dimensional array script. Example:

Table 1 >> Campos id and name

Table 2 >> Campos id and telephone

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 script id, several columns; example: 'name', 'telephone' etc, as shown in Situation?

Thank you!

  • Bash does not support (directly) multidementional arrays. You can work around the problem (1) by using (strings, texts, etc) multilevel separators (EX: CVS), (even within bash Arrays values), (2) using XML, JSON or (3) by migrating to perl, python, ruby, etc.

  • Thank you @Jjoao!

  • If you need help with examples of any of the "proposals", says!

  • @JJOAO thanks!! I was needing to assemble a script and meet this demand for the link below, but in the end everything worked out. Thank you very much!! http://answall.com/questions/86491/manter-configura%C3%A7%C3%B5es-ao-alterar-url-wordpress-tema-Adventure-Organic-them/86665#86665

1 answer

1

From what I understand, you want an array that stores information id and name. If it is...

Creating bash array 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: - "Hey array, how many elements are stored in you?"

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

#!/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 so recommended to use the eval, but beauty... that’s just one example! ;)

Using the code below demonstrates a 'technique' to print only 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, no 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 catch the id of the tables, I have to add them to the array so that I can take the exact size and make a loop from 1 (not from scratch! ) up to the maximum array size. This is what I did using while: [[ "$id" -lt "${#array[@]}" ]].

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.