0

I want to have parse nested configurations in Bash, like below:

[foo]
   [bar]
      key="value"
   [baz]
      key="value"

I tried this .ini parser but it does not support nesting. Later I found out that nesting isn't allowed in .ini files.

I searched for a YAML parser for bash, but I couldn't find a lot. Nested configuration parsing in bash seems to me as a basic problem, so I guess a trivial solution exists, but I could not find one. Does a triivial solution for parsing nested configuration in Bash exists? If yes, which one?

EDIT

I want to write a script/program for automated backup and restore of databases. The configuration needs to flexible so that I can select databases on different hosts, with different users and passwords and with different backup intervals. Oh, and I want to learn bash. But I am starting to think that Bash is not the right tool for my problem.

OrangeTux
  • 11,142
  • 7
  • 48
  • 73

2 Answers2

2

Bash is not the right language for this. There are no nested arrays, and dynamic variable assignment is a bit of a mine field compared to languages like Python and Ruby. That said, it sounds like you're specifying the format and parser yourself, so you could simply use a hierarchical naming scheme for your configuration:

foo_bar_key="value"
foo_baz_key="value"
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • I don't think it's right to say Bash is the wrong language for this. Sure, it isn't as straigntforward as Python or Ruby but that's only because they have libraries built that hide the complexities. [You can write a parser in bash](http://stackoverflow.com/a/31705244) that does this and put it in a library file so that parsing becomes a one-liner. – starfry Jul 30 '15 at 07:15
  • Sorry, that parser is bordering on incomprehensible. No thanks. – l0b0 Jul 30 '15 at 16:58
0

I wrote a Yamlesque parser in response to this similar question.

It will parse

foo:
  bar:
    key: value
  baz:
    key: value

into bash associative arrays. 100% Bash, but it needs to be Bash 4.x.

Community
  • 1
  • 1
starfry
  • 9,273
  • 7
  • 66
  • 96