Skip to content

Instantly share code, notes, and snippets.

@mkaito
Created February 22, 2012 15:48
Show Gist options
  • Save mkaito/1885631 to your computer and use it in GitHub Desktop.
Save mkaito/1885631 to your computer and use it in GitHub Desktop.

Revisions

  1. Kaito Michishige created this gist Feb 22, 2012.
    27 changes: 27 additions & 0 deletions readrc.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/env zsh
    # An example script that reads a colon separated rc file, given on argv
    # Line format expected:
    # option: value
    # Mind the colon and space separating option name from the value.
    # Adjust the parameter expansion as needed if you change the line format.

    # - read <var> will read a line at a time.
    # - Piping the file name into the while block reads the file into the read call.
    # - typeset -A <var> explicitly marks a hash for ZSH.
    # declare -A <var> is the same for bash.
    # - Fancypants parameter expansion gives us opts[option]=value.
    # - Quoting the value allows is to be any valid string, which generally means
    # that the value will be anything between the colon and the next newline character.

    if [[ -f $1 ]]; then
    typeset -A opts
    while read l; do
    opts[${l%%:*}]="${l##*: }"
    done < $1
    fi

    echo "Number of found options: ${#opts[*]}"

    for k in ${(k)opts}; do
    echo "$k => ${opts[$k]}"
    done