Skip to content

Instantly share code, notes, and snippets.

@DarkMukke
Created January 27, 2016 10:46
Show Gist options
  • Select an option

  • Save DarkMukke/9f9cd3441c921a272acf to your computer and use it in GitHub Desktop.

Select an option

Save DarkMukke/9f9cd3441c921a272acf to your computer and use it in GitHub Desktop.

Revisions

  1. DarkMukke created this gist Jan 27, 2016.
    37 changes: 37 additions & 0 deletions iniLoop.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/usr/bin/env bash

    #
    # # # example.ini
    # [goedhuis-dev]
    # USER = juno
    # HOST = gh.juno.is
    #
    # [goedhuis-stag]
    # USER = juno
    # HOST = ghstaging.juno.is
    #


    CONFIGFILE="example.ini";
    REMOTECOMMAND="ls -All";

    _SECTIONS=`cat ${CONFIGFILE} | grep -o -P "\[([a-zA-Z0-9-]+)\]" | tr -d [] | sed ':a;N;$!ba;s/\n/ /g'`

    ini_parser() {
    FILE=$1
    SECTION=$2
    eval $(sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
    -e 's/[;#].*$//' \
    -e 's/[[:space:]]*$//' \
    -e 's/^[[:space:]]*//' \
    -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
    < $FILE \
    | sed -n -e "/^\[$SECTION\]/I,/^\s*\[/{/^[^;].*\=.*/p;}")
    }


    # A sections array that we'll loop through
    for SEC in $_SECTIONS; do
    ini_parser ${CONFIGFILE} ${SEC};
    echo "ssh $USER@$HOST"
    done