Skip to content
Snippets Groups Projects
Commit 86b7a80f authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

added colored text, handle multiple replace soures

parent 2dad2153
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch> # 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch>
# 2021-05-07 v1.0 <axel.hahn@iml.unibe.ch> multiple replacement files
# ====================================================================== # ======================================================================
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -15,12 +16,35 @@ ...@@ -15,12 +16,35 @@
tplfile=$1 tplfile=$1
outfile=$2 outfile=$2
replacefile=$3 shift 2
replacefile=$*
test -z "$replacefile" && replacefile="replace*.txt" test -z "$replacefile" && replacefile="replace*.txt"
tmpfile=/tmp/myreplacement.tmp tmpfile=/tmp/myreplacement.tmp
usage="`basename $0` TEMPLATE-FILE OUTFILE [optional: REPLACE-DATA-FILE]" usage="`basename $0` TEMPLATE-FILE OUTFILE [optional: REPLACE-DATA-FILE]"
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# show colored text + line break
function _colmessage(){
echo -e "\033${1}${2}\033[0m"
}
# show red error message
function _err(){
_colmessage "[31m" "$1"
}
# show green message
function _ok(){
_colmessage "[32m" "$1"
}
# show green message
function _warn(){
_colmessage "[33m" "$1"
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -36,20 +60,20 @@ echo ...@@ -36,20 +60,20 @@ echo
# --- parameter checks # --- parameter checks
if [ -z "$tplfile" -o -z "$outfile" ]; then if [ -z "$tplfile" -o -z "$outfile" ]; then
echo "ERROR: missing params." _err "ERROR: missing params."
echo $usage echo $usage
exit 1 exit 1
fi fi
if [ ! -f "$tplfile" ]; then if [ ! -f "$tplfile" ]; then
echo "ERROR: template [$tplfile] does not exist." _err "ERROR: template [$tplfile] does not exist."
echo $usage echo $usage
exit 2 exit 2
fi fi
ls $replacefile >/dev/null ls $replacefile >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "ERROR: replacement data file [$replacefile] does not exist." _err "ERROR: replacement data file [$replacefile] does not exist."
echo $usage echo $usage
exit 2 exit 2
fi fi
...@@ -60,7 +84,7 @@ cat $tplfile > $tmpfile ...@@ -60,7 +84,7 @@ cat $tplfile > $tmpfile
# --- apply replace rules # --- apply replace rules
grep "^[a-zA-Z]" $replacefile | grep "=" | while read line grep "^[a-zA-Z]" $replacefile | grep "=" | while read line
do do
key=$( echo $line | cut -f 1 -d "=" ) key=$( echo $line | cut -f 2- -d ":" | cut -f 1 -d "=" )
val=$( echo $line | cut -f 2- -d "=" ) val=$( echo $line | cut -f 2- -d "=" )
regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>" regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>"
...@@ -75,7 +99,7 @@ echo ...@@ -75,7 +99,7 @@ echo
echo --- changes: echo --- changes:
diff "$tplfile" "$tmpfile" diff "$tplfile" "$tmpfile"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:" _err "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:"
cat $replacefile cat $replacefile
exit 3 exit 3
fi fi
...@@ -85,9 +109,22 @@ mv -f "${tmpfile}" "${outfile}" ...@@ -85,9 +109,22 @@ mv -f "${tmpfile}" "${outfile}"
echo echo
echo --- output file: echo --- output file:
ls -l "${outfile}" && exit 0 ls -l "${outfile}"
if [ $? -ne 0 ]; then
echo "ERROR: unable to write target file $outfile." _err "ERROR: unable to write target file $outfile."
exit 4 exit 4
fi
grep '<\%\=\ *\@replace\[\"' ${outfile} >/dev/null
if [ $? -eq 0 ]; then
echo
_warn "WARNING: Maybe you need to verify replacements (can be a false positive)"
grep '<\%\=\ *\@replace\[\"' ${outfile}
else
_ok "OK"
fi
exit 0
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment