Skip to content

Instantly share code, notes, and snippets.

@fmoessbauer
Last active February 13, 2018 08:17
Show Gist options
  • Select an option

  • Save fmoessbauer/3695a4c32a825f9f9ea9dc6a754d5e24 to your computer and use it in GitHub Desktop.

Select an option

Save fmoessbauer/3695a4c32a825f9f9ea9dc6a754d5e24 to your computer and use it in GitHub Desktop.

Revisions

  1. fmoessbauer revised this gist Feb 13, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions replacer.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    # Trivial solution to substitute placeholders in config files
    # Trivial solution to replace placeholders in config files
    # Finds all .tpl files, replaces keys and stores the result
    # without .tpl
    #
    @@ -13,9 +13,10 @@ function replace_in_file {
    FILENAME=${1//.tpl/}
    [[ $FILENAME == $1 ]] && exit 1
    echo "Process $FILENAME"
    cp $1 $FILENAME
    while read p; do
    KEYVAL=(${p//=/ })
    sed -e "s/<<${KEYVAL[0]}>>/${KEYVAL[1]}/g" $1 > $FILENAME
    sed -i -e "s/<<${KEYVAL[0]}>>/${KEYVAL[1]}/g" $FILENAME
    done < $2
    }

  2. fmoessbauer created this gist Feb 12, 2018.
    27 changes: 27 additions & 0 deletions replacer.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/bin/bash
    # Trivial solution to substitute placeholders in config files
    # Finds all .tpl files, replaces keys and stores the result
    # without .tpl
    #
    # Usage: replacer.sh <keyfile>
    #
    # Keyfile format:
    # key=value
    # key2=value2

    function replace_in_file {
    FILENAME=${1//.tpl/}
    [[ $FILENAME == $1 ]] && exit 1
    echo "Process $FILENAME"
    while read p; do
    KEYVAL=(${p//=/ })
    sed -e "s/<<${KEYVAL[0]}>>/${KEYVAL[1]}/g" $1 > $FILENAME
    done < $2
    }

    [ -e "$1" ] || exit 1
    echo "Secrets file: $1"

    find . -iname "*.tpl" | while read p; do
    replace_in_file $p $1
    done