Skip to content

Instantly share code, notes, and snippets.

@ngoma84
Forked from splaspood/bash_iniparse.sh
Created February 22, 2018 14:43
Show Gist options
  • Select an option

  • Save ngoma84/330bfcd69bc9f2f8f0723cbcd49e3af4 to your computer and use it in GitHub Desktop.

Select an option

Save ngoma84/330bfcd69bc9f2f8f0723cbcd49e3af4 to your computer and use it in GitHub Desktop.

Revisions

  1. @splaspood splaspood created this gist Dec 13, 2011.
    17 changes: 17 additions & 0 deletions bash_iniparse.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    cfg.parser () {
    fixed_file=$(cat $1 | sed 's/ = /=/g') # fix ' = ' to be '='
    IFS=$'\n' && ini=( $fixed_file ) # convert to line-array
    ini=( ${ini[*]//;*/} ) # remove comments
    ini=( ${ini[*]/#[/\}$'\n'cfg.section.} ) # set section prefix
    ini=( ${ini[*]/%]/ \(} ) # convert text2function (1)
    ini=( ${ini[*]/=/=\( } ) # convert item to array
    ini=( ${ini[*]/%/ \)} ) # close array parenthesis
    ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
    ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
    ini[0]='' # remove first element
    ini[${#ini[*]} + 1]='}' # add the last brace
    eval "$(echo "${ini[*]}")" # eval the result
    }

    cfg.parser '/etc/my.ini'
    cfg.section.images