I am using Bash 4.3 on linux.
I have this simple YAML-esque data file:
products:
product1:
name: "Product one"
price: 100
product2:
name: "Product two"
price: 200
myList:
- one
- two
And I need a shell function that, taking the above YAML file as input, can generate and then execute the below Bash code:
unset products product1 product2
# declare the associative arrays
declare -A product1
declare -A product2
# define the data
product1=(
[name]="Product 1"
[price]=100
)
product2=(
[name]="Product 2"
[price]=200
)
myList=(one two)
# declare the arrays which will contain the names of our associative arrays
products=(product1 product2)
Once I have this wonderful function, I will use the YAML files to automatically generate data, to be used in my custom CMS templating system like so:
{{#foreach product in products}}
<h3>{{product.name | uppercase}}</h3>
* {{product.price | money_with_currency £ GBP | without_trailing_zeros}}
{{/foreach}}
I have already tried various YAML parsers, but have not found one that can generate the associative arrays that I need, and some simply didn't work at all (for me, at least):
- https://github.com/ArtBIT/bash-yaml
- https://github.com/luodongseu/shyaml
- https://github.com/binaryphile/y2s
- https://github.com/Hashfyre/yamlparser
- https://github.com/ash-shell/yaml-parse
- https://github.com/jasperes/bash-yaml
- https://github.com/mrbaseman/parse_yaml
- https://github.com/azohra/yaml.sh
- https://github.com/Minlison/yaml-parser
- https://gist.github.com/pkuczynski/8665367
Most of these, as far as I understand their usage generate things like product_product1_name="foo"
:(
yaml.sh
, which you linked in the question, is a surprisingly good parser. It's a lot easier to convert its output into the format you need than to do anything else....emits as output: