I need to have a very basic template system in shell to port a windows installer to linux. So I can not change the syntax of the template variables.
I want to take specific environment variables (starting with $ENV_PREFIX) and build a sed script of the form s/@@@${TEMPLATE_VARIABLE}@@@/$VALUE_OF_ENVIRONMENT_VARIABLE/
.
$TEMPLATE_VARIABLE is the name of the environment variable with the $ENV_PREFIX stripped off.
#!/usr/bin/env sh # no bashisms please!
TEMPLATE_FILE=$1
ENV_PREFIX="DMTMPL_"
SED_SCRIPT=""
OIFS="$IFS"
IFS=$'\012'
for VAR in "$(env | grep "^${ENV_PREFIX}")"
do
VARIABLE=$(echo $VAR | cut -d= -f1 | cut -d_ -f2-)
SED_SCRIPT="${SED_SCRIPT}\ns/${VARIABLE}/${???}/g"
done
echo ${SED_SCRIPT}
# invoke sed with $SED_SCRIPT on $TEMPLATE_FILE
Now I have all kinds of problems with this script:
- I tried to do
env --null
, but could not combine this with the following grep. - The for loop does not exactly handle one environment variable in every loop
- I did not manage to have $SED_SCRIPT nicely separated with newlines
- How do I access the value of a variable when the variable name is stored in another variable? That's what I need to put where the ??? are in the script.
I thought I'd write a quick shell script for this but now I remember why I hate shell scripting... :-(
These Questions seem to answer part of my question: