Array as data structure in bash (configuration file)?

I wrote a bash script that process multiple files. Now I want to add support for the configuration file. This is my requested data structure:

Array ( [0] => Array ( [name] => datset1 [path] => /var/lib/bliTool/ds1 [type] => cvs ) [1] => Array ( [name] => datset2 [path] => /var/lib/bliTool/ds2 [type] => xml ) [2] => Array ( [name] => datset3 [path] => /home/igor/test/ds3 [type] => cvs ) ) 

Q1 Is such a data structure possible in bash? Are there any other recommendations? Remember that this should be in the configuration file ...

Q2: I think of one configuration file for each set, for example

 /etc/myApp/ /etc/myApp/myApp.conf /etc/myApp/datasets.d/ /etc/myApp/datasets.d/ds1.conf /etc/myApp/datasets.d/ds2.conf /etc/myApp/datasets.d/dsN.conf 

and each /etc/myApp/datasets.d/dsN.conf file will look like

 name=The DS name path=/the/path/to/the/ds/files type=thetype 

What do you recommend? Is there a way to do everything in one file?

Q3: I want to support multiple path values ​​for each set. I could support something like

 path="/first/path /second/path" 

But I think that I will have problems with spaces, so I have to enter a delimeter like

 path="/first/path:/second/path" 

to break the line.

Or is there a better way?

+6
source share
2 answers

You cannot nest data rows in bash . In the best case, you can store the names of associative arrays in an array and go through workarounds to access them.

 $ declare -A aa0=([name]=dataset1 [path]=/var/lib/bliTool/ds1 [type]=cvs ) $ declare -A aa1=([name]=dataset2 [path]=/var/lib/bliTool/ds2 [type]=xml ) $ declare -A aa2=([name]=dataset3 [path]=/home/igor/test/ds3 [type]=cvs ) $ declare -a array=( aa0 aa1 aa2 ) $ tmp=aa0[name] $ echo ${!tmp} dataset1 

In the second question, of course, you can determine the format of the configuration file with partitions, but you will need to write a parser that can handle it. Other languages ​​typically have a library available for analyzing rich configuration files.

For multiple paths to a variable, stick with : Theoretically, any separator can be used as part of the path name component, so a separator should be specified if it is part of the path. But since PATH uses : as its delimiter, there is a historical realization that : not a big character to use in the path name and that it needs to be specified in the PATH parameters.

 path="/first/poor\:path\:name:/second/bad\:path\:name" 

Then you will need to process the snapshot :

+8
source

I came to a similar situation, and my solution is to use different IFSs at different levels, this is somehow similar to the idea of ​​chepner. Code and sample can be found here https://github.com/pppoe/Nested-Array-Bash/

+1
source

All Articles