bash(配置文件)中的类似数据结构的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15684646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array like data structure in bash (config file)?
提问by Igor
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:
我写了一个 bash 脚本,它处理多个文件。我现在想添加对配置文件的支持。这是我想要的数据结构:
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
)
)
Q1Is such a data structure possible within bash? Are there other recommendations? Remember, this should be in a config file...
Q1这样的数据结构在 bash 中可行吗?还有其他推荐吗?请记住,这应该在配置文件中...
Q2:I am thinking about one configuration file per 'set' like
Q2:我正在考虑每个“集合”一个配置文件,例如
/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.conffile would look like
每个/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:I want to support multiple path values per set. I could support something like
Q3:我想支持每组多个路径值。我可以支持类似的东西
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"
to split the string.
拆分字符串。
Or is there a better way?
或者,还有更好的方法?
回答by chepner
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.
您不能将数据结构嵌套在bash. 充其量,您可以将关联数组的名称存储在数组中,并跳过间接环来访问它们。
$ 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.
对于第二个问题,当然可以定义带有部分的配置文件格式,但是您需要编写一个可以处理它的解析器。其他语言通常有一个库可用于解析丰富的配置文件格式。
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 PATHuses :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使用:作为其定界符,因此历史意识:在路径名中不是一个很好的字符,并且需要在类似PATH参数中引用它。
path="/first/poor\:path\:name:/second/bad\:path\:name"
Then it will be up to your application to process the back-slashed :.
然后由您的应用程序来处理反斜杠:.
回答by mr.pppoe
I came into a similar situation and my solution is to use different IFS within different level, it is somehow similar to chepner's idea. The code and sample can be found here https://github.com/pppoe/Nested-Array-Bash/
我遇到了类似的情况,我的解决方案是在不同级别使用不同的 IFS,这与 chepner 的想法有些相似。代码和示例可以在这里找到https://github.com/pppoe/Nested-Array-Bash/

