阵列状的数据结构在bash(配置文件)? [英] Array like data structure in bash (config file)?
问题描述
我写了一个bash脚本,它处理多个文件。我现在想添加一个配置文件的支持。这是我想要的数据结构:
I've written a bash script, which process multiple files. I now want to add support for a config file. This is my wanted 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 是这样的数据结构可能的bash内?还有没有其他的建议?请记住,这应该是在配置文件...
Q1 Is such a data structure possible within bash? Are there other recommendations? Remember, this should be in a config file...
Q2:我想每个'设置'关于一个配置文件像
Q2: I am thinking about one configuration file per 'set' like
/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
和每 /etc/myApp/datasets.d/dsN.conf 的文件看起来像
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:我要支持元一套多路径值。我可以支持像
Q3: I want to support multiple path values per set. I could support something like
path="/first/path /second/path"
不过,我想我会得到与空间的麻烦,所以我应该像引进
But I think I'll get trouble with spaces, so I should introduce a delimeter like
path="/first/path:/second/path"
要分割字符串。
还是有更好的办法?
推荐答案
您不能在庆典
窝数据strutures。在最好的情况,你可以关联数组的名称存储在数组中,并通过间接跳火圈来访问它们。
You can't nest data strutures in bash
. At best, you can store the names of associative arrays in an array, and jump through indirection hoops 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
对于第二个问题,它肯定可以定义与部分的配置文件格式,但你需要编写一个解析器能够处理它。其他语言通常可用来解析丰富的配置文件格式的库。
For the second question, it's certainly possible to define a configuration file format with sections, but you'll need to write a parser that can process it. Other languages typically have a library available to parse rich configuration file formats.
至于多条路径,坚持使用:
。在理论上,任何定界符可以作为一个路径名组件的一部分,所以分隔符需要,如果它是一个路径的一部分被引用。但是,由于 PATH
使用:
作为分隔符,没有历史意识的:
是不是一个伟大的人物在路径名来使用,而且它需要 PATH被引用
样的参数。
As far as multiple paths per variable, stick with :
. In theory, any delimiter could be used as part of a path name component, and so the delimiter needs to be quoted if it is part of a path. But since PATH
uses :
as its delimiter, there is historical awareness that :
is not a great character to use in a path name, and that it needs to be quoted in PATH
-like parameters.
path="/first/poor\:path\:name:/second/bad\:path\:name"
然后,它会到你的应用程序来处理背削减:
这篇关于阵列状的数据结构在bash(配置文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!