From f0adbc54d4c61c14ac101812eeccf95c3cccadf5 Mon Sep 17 00:00:00 2001 From: Erik Mulder Date: Tue, 29 Jan 2019 16:44:40 +0100 Subject: [PATCH 1/4] Fix typo in comment --- templater.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templater.sh b/templater.sh index e96f2ff..3d08aa5 100755 --- a/templater.sh +++ b/templater.sh @@ -122,7 +122,7 @@ var_value() { replaces="" # Reads default values defined as {{VAR=value}} and delete those lines -# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}} +# These are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}} # You can even reference variables defined in the template before defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//') From 89a418307e1cecdc41ddef37827f6ae36a53a827 Mon Sep 17 00:00:00 2001 From: Erik Mulder Date: Tue, 29 Jan 2019 16:45:49 +0100 Subject: [PATCH 2/4] Removed all trailing whitespace --- templater.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/templater.sh b/templater.sh index 3d08aa5..7f11d6f 100755 --- a/templater.sh +++ b/templater.sh @@ -35,7 +35,7 @@ config_file="" print_only="false" silent="false" -usage="${PROGNAME} [-h] [-d] [-f] [-s] -- +usage="${PROGNAME} [-h] [-d] [-f] [-s] -- where: -h, --help @@ -48,13 +48,13 @@ where: Don't print warning messages (for example if no variables are found) examples: - VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt + VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt ${PROGNAME} test.txt -f my-variables.txt ${PROGNAME} test.txt -f my-variables.txt > new-test.txt" if [ $# -eq 0 ]; then echo "$usage" - exit 1 + exit 1 fi if [[ ! -f "${1}" ]]; then @@ -72,7 +72,7 @@ if [ "$#" -ne 0 ]; then -h|--help) echo "$usage" exit 0 - ;; + ;; -p|--print) print_only="true" ;; @@ -108,12 +108,12 @@ fi if [ "${config_file}" != "" ]; then if [[ ! -f "${config_file}" ]]; then echo "The file ${config_file} does not exists" >&2 - echo "$usage" + echo "$usage" exit 1 fi source "${config_file}" -fi +fi var_value() { eval echo \$$1 @@ -153,7 +153,7 @@ fi # Replace all {{VAR}} by $VAR value for var in $vars; do - value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped + value=$(var_value $var | sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g") # '&' and is escaped if [[ -z "$value" ]]; then if [ $silent == "false" ]; then echo "Warning: $var is not defined and no default is set, replacing by empty" >&2 @@ -162,7 +162,7 @@ for var in $vars; do # Escape slashes value=$(echo "$value" | sed 's/\//\\\//g'); - replaces="-e 's/{{$var}}/${value}/g' $replaces" + replaces="-e 's/{{$var}}/${value}/g' $replaces" done escaped_template_path=$(echo $template | sed 's/ /\\ /g') From a78e31cd5395239f26a60a976c76957647c45a50 Mon Sep 17 00:00:00 2001 From: Erik Mulder Date: Tue, 29 Jan 2019 16:47:04 +0100 Subject: [PATCH 3/4] Replaced use of backticks with $() --- templater.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templater.sh b/templater.sh index 7f11d6f..39ee06e 100755 --- a/templater.sh +++ b/templater.sh @@ -128,7 +128,7 @@ defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' for default in $defaults; do var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+") - current=`var_value $var` + current=$(var_value $var) # Replace only if var is not set if [[ -z "$current" ]]; then @@ -145,7 +145,7 @@ vars=$(echo $vars | sort | uniq) if [[ "$print_only" == "true" ]]; then for var in $vars; do - value=`var_value $var` + value=$(var_value $var) echo "$var = $value" done exit 0 From ba2b75257e2127278d7ce066f13e1789e1175dec Mon Sep 17 00:00:00 2001 From: Erik Mulder Date: Tue, 29 Jan 2019 16:48:35 +0100 Subject: [PATCH 4/4] Don't add default value to list of variables --- templater.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/templater.sh b/templater.sh index 39ee06e..ade189e 100755 --- a/templater.sh +++ b/templater.sh @@ -137,8 +137,6 @@ for default in $defaults; do # remove define line replaces="-e '/^{{$var=/d' $replaces" - vars="$vars -$current" done vars=$(echo $vars | sort | uniq)