OrangeTux
OrangeTux

Reputation: 11461

Trivial solution for parsing nested configuration in Bash?

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.

Upvotes: 0

Views: 823

Answers (2)

starfry
starfry

Reputation: 9943

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.

Upvotes: 0

l0b0
l0b0

Reputation: 58788

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"

Upvotes: 2

Related Questions