Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of arrays in bash

Tags:

arrays

bash

shell

I'm attempting to read an input file line by line which contains fields delimited by periods. I want to put them into an array of arrays so I can loop through them later on. The input appears to be ok, but 'pushing' that onto the array (inData) doesn't appear to be working.

The code goes :

Input file:  GSDB.GOSALESDW_DIST_INVENTORY_FACT.MONTH_KEY GSDB.GOSALESDW_DIST_INVENTORY_FACT.ORGANIZATION_KEY   infile=${1}  OIFS=$IFS IFS=":"  cat ${infile} | while read line do       line=${line//\./:}       inarray=(${line}) #      echo ${inarray[@]} #      echo ${#inarray[@]}       #      echo ${inarray[0]} #      echo ${inarray[1]} #      echo ${inarray[2]}        inData=("${inData[@]}" "${inarray[@]}") done  IFS=$OIFS  echo ${#inData[@]}     for ((i = 0; i < ${#inData[@]}; i++)) do  echo $i     for ((j = 0; j < ${#inData[$i][@]}; j++))     do        echo ${inData[$i][$j]}     done done 
like image 490
jaybee Avatar asked Sep 07 '12 11:09

jaybee


People also ask

Can you have an array of arrays in bash?

Still, multidimensional arrays aren't supported by bash, and we can't get array components that are also arrays. Fortunately, multidimensional arrays can be simulated. This article will provide some illustrations of the simulation of an array of arrays in a bash script.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is an associative array in bash?

Bash, however, includes the ability to create associative arrays, and it treats these arrays the same as any other array. An associative array lets you create lists of key and value pairs, instead of just numbered values. You can assign values to arbitrary keys: $ declare -A userdata.

Is there array in bash?

Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously.


2 Answers

Field nest box in bash but it can not circumvent see the example.

#!/bin/bash  # requires bash 4 or later; on macOS, /bin/bash is version 3.x, # so need to install bash 4 or 5 using e.g. https://brew.sh  declare -a pages  pages[0]='domain.de;de;https' pages[1]='domain.fr;fr;http'  for page in "${pages[@]}" do     # turn e.g. 'domain.de;de;https' into     # array ['domain.de', 'de', 'https']     IFS=";" read -r -a arr <<< "${page}"      site="${arr[0]}"     lang="${arr[1]}"     prot="${arr[2]}"     echo "site : ${site}"     echo "lang : ${lang}"     echo "prot : ${prot}"     echo done 
like image 114
Lukáš Kříž Avatar answered Oct 12 '22 23:10

Lukáš Kříž


Bash has no support for multidimensional arrays. Try

array=(a b c d) echo ${array[1]} echo ${array[1][3]} echo ${array[1]exit} 

For tricks how to simulate them, see Advanced Bash Scripting Guide.

like image 26
choroba Avatar answered Oct 13 '22 00:10

choroba