diff --git a/bin/create_config.sh b/bin/create_config.sh index 8e883c0f38995c63e10cb2c0d683465a5c5ec755..511b4770f2adee3dca12d4a25ca3c34d28bc844d 100755 --- a/bin/create_config.sh +++ b/bin/create_config.sh @@ -8,6 +8,7 @@ # ---------------------------------------------------------------------- # 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch> # 2021-05-07 v1.0 <axel.hahn@iml.unibe.ch> multiple replacement files +# 2021-05-27 v1.1 <axel.hahn@iml.unibe.ch> Warn on missing replacements; handle Ruby vars with single and double quotes # ====================================================================== # ---------------------------------------------------------------------- @@ -78,10 +79,11 @@ if [ $? -ne 0 ]; then exit 2 fi -echo --- Replacements to apply: -grep "^[a-zA-Z]" $replacefile | grep "=" -if [ $? -ne 0 ]; then - _err "ERROR: No replacements were found ... wrong syntax? Use KEY = VALUE." +typeset -i iReplacements=$( grep "^[a-zA-Z]" $replacefile | grep "=" | wc -l ) +echo --- Replacements to apply: $iReplacements +if [ $iReplacements -eq 0 ]; then + _err "ERROR: No replacements were found ... maybe a wrong syntax?" + _err " Use KEY=VALUE; no spaces in front; now quotes around VALUE" exit 2 fi @@ -94,19 +96,27 @@ do key=$( echo $line | cut -f 2- -d ":" | cut -f 1 -d "=" ) val=$( echo $line | cut -f 2- -d "=" ) - regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>" - grep "$regex" $tmpfile >/dev/null || echo "WARNING: template [$tplfile] has no placeholder key [$key]" - # Ruby erb template syntax: <% @replace["mykey"] %> - # sed -i "s#<\%\ *\@replace\[\"$key\"\]\ *\%>#$val#g" $tmpfile - sed -i "s#$regex#$val#g" $tmpfile + # Ruby erb template syntax to write a variable: <% @replace["mykey"] %> + # Here are 2 regex to handle single and double quotes + regex1="<\%\=\ *\@replace\[\'$key\'\]\ *\%>" + regex2="<\%\=\ *\@replace\[\"$key\"\]\ *\%>" + + # Show a warning if both regex do not match + grep "$regex1" $tmpfile >/dev/null \ + || grep "$regex2" $tmpfile >/dev/null \ + || echo "WARNING: template [$tplfile] has no placeholder key [$key]" + + sed -i "s#$regex1#$val#g" $tmpfile + sed -i "s#$regex2#$val#g" $tmpfile done # --- verify changes echo -echo --- changes: +echo --- Changes: diff "$tplfile" "$tmpfile" if [ $? -eq 0 ]; then _err "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:" + echo Content of relacement files: cat $replacefile exit 3 fi @@ -115,23 +125,24 @@ fi mv -f "${tmpfile}" "${outfile}" echo -echo --- output file: +echo --- Output file: ls -l "${outfile}" if [ $? -ne 0 ]; then _err "ERROR: unable to write target file $outfile." exit 4 fi -grep '<\%\=\ *\@replace\[\"' ${outfile} >/dev/null +echo --- Check missing replacements +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} + _warn "WARNING: Maybe you need to verify replacements (hmmm, could be a false positive too)" + grep '<\%\=\ *\@replace\[' ${outfile} else _ok "OK" fi - exit 0 +exit 0 # ----------------------------------------------------------------------