A
A
Alexey Yarkov2017-03-12 21:23:20
bash
Alexey Yarkov, 2017-03-12 21:23:20

Is there a multidimensional array syntax in bash?

You have to keep 3 arrays of the same length.

namelist=("Россия" "Россия-2" "Латвия" "США" "Германия" "Швеция" "Нидерланды" "Япония")

latins=("russia" "russia-2" "latvia" "usa" "germany" "sweden" "netherlands" "japan")

loadlist=("Russia_freeopenvpn" "Russia-2_freeopenvpn" "Latvia_freeopenvpn" "freeopenvpn_USA" "Germany_freeopenvpn" "Sweden_freeopenvpn" "Netherlands_freeopenvpn" "freeopenvpn_Japan")

I would like to sort out this disgrace somehow.
#!/bin/bash

set -e

arr=(\
    ("Россия" "russia" "Russia_freeopenvpn")\
    ("Россия-2" "russia-2" "Russia-2_freeopenvpn")\
    ("Латвия" "latvia" "Latvia_freeopenvpn")\
    ("США" "usa" "freeopenvpn_USA")\
    ("Германия" "germany" "Germany_freeopenvpn")\
    ("Швеция" "sweden" "Sweden_freeopenvpn")\
    ("Нидерланды" "netherlands" "Netherlands_freeopenvpn")\
    ("Япония" "japan" "freeopenvpn_Japan")\
)

for ((i=0; i < ${#arr[@]}; i++))
do
    echo "${arr[$i]}"
done

Output error:
$ ./test.sh
./test.sh: строка 6: ошибка синтаксиса около неожиданной лексемы `('

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-03-12
@yarkov

no, bash does not support multidimensional arrays.
But I see that all your names do not contain spaces, so store them in lines

#/bin/bash

arr=(
"Россия russia Russia_freeopenvpn"
"Россия-2 russia-2 Russia-2_freeopenvpn"
"Латвия latvia Latvia_freeopenvpn"
"США usa freeopenvpn_USA"
"Германия germany Germany_freeopenvpn"
"Швеция sweden Sweden_freeopenvpn"
"Нидерланды netherlands Netherlands_freeopenvpn"
"Япония japan freeopenvpn_Japan")
IFS=' ' read -r -a array <<< "${arr[1]}"

echo "name: ${array[0]}, smallname: ${array[1]}, vpnname: ${array[2]}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question