[beast: 3/11] MISC: applyenv.sh: add script to substitute @ENVIRONMENTVARIABLE@ in input file



commit 70b4fc3365ff296764e082a436ccad92bdb6f5bf
Author: Tim Janik <timj gnu org>
Date:   Wed Oct 19 10:14:44 2016 +0200

    MISC: applyenv.sh: add script to substitute @ENVIRONMENTVARIABLE@ in input file
    
    Signed-off-by: Tim Janik <timj gnu org>

 misc/applyenv.sh |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
---
diff --git a/misc/applyenv.sh b/misc/applyenv.sh
new file mode 100755
index 0000000..713ae93
--- /dev/null
+++ b/misc/applyenv.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+set -e
+
+# NOTE: avoid using unprefixed environment variables in this script, as they could affect output 
substitutions
+
+applyenv__SCRIPTNAME___="$(basename "$0")" ; die() { e="$1"; shift; echo "$applyenv__SCRIPTNAME___: $*" >&2; 
exit "$e"; }
+
+applyenv__input___="$1" ; shift || :
+
+test -r "$applyenv__input___" || die 1 "Usage: applyenv.sh <inputfile.in>"
+
+applyenv__sedprogram___=""
+for applyenv__var___ in \
+  $(grep -oE '@[0123456789abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ]+@' "$applyenv__input___" | 
sort | uniq | sed s/@//g)
+do
+  applyenv__escaped___=$(printf %s "${!applyenv__var___}" | sed 's/[&\\|]/\\&/g') # escape for sed 's' 
embedding
+  applyenv__sedprogram___="$applyenv__sedprogram___; s|@$applyenv__var___@|$applyenv__escaped___|g"
+done
+set -x
+sed "$applyenv__sedprogram___" "$applyenv__input___"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]