Skip to content

Instantly share code, notes, and snippets.

@jim0409
Forked from pkuczynski/LICENSE
Created May 27, 2020 09:30
Show Gist options
  • Save jim0409/da94b09838b878409478a560939d146d to your computer and use it in GitHub Desktop.
Save jim0409/da94b09838b878409478a560939d146d to your computer and use it in GitHub Desktop.

Revisions

  1. @pkuczynski pkuczynski revised this gist Jan 19, 2016. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    . parse_yaml.sh

    # read yaml file
    eval $(parse_yaml config.yml "config_")
    eval $(parse_yaml zconfig.yml "config_")

    # access yaml content
    echo $config_development_database
    File renamed without changes.
  2. @pkuczynski pkuczynski created this gist Jan 28, 2014.
    6 changes: 6 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    development:
    adapter: mysql2
    encoding: utf8
    database: my_database
    username: root
    password:
    16 changes: 16 additions & 0 deletions parse_yaml.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #!/bin/sh
    parse_yaml() {
    local prefix=$2
    local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
    sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
    -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
    awk -F$fs '{
    indent = length($1)/2;
    vname[indent] = $2;
    for (i in vname) {if (i > indent) {delete vname[i]}}
    if (length($3) > 0) {
    vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
    printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
    }
    }'
    }
    10 changes: 10 additions & 0 deletions test.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/sh

    # include parse_yaml function
    . parse_yaml.sh

    # read yaml file
    eval $(parse_yaml config.yml "config_")

    # access yaml content
    echo $config_development_database