Skip to content

Instantly share code, notes, and snippets.

@andrioid
Created June 13, 2014 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrioid/f5e7a6f20d948b0f273a to your computer and use it in GitHub Desktop.
Save andrioid/f5e7a6f20d948b0f273a to your computer and use it in GitHub Desktop.
Simple search/replace config file script in Bash
#!/bin/bash
# Replaces params with values and outputs
# Note: Not really designed for complex variables. So beware of sed
USAGE="configparse <infile> [param1=value1 param2=value2 .. paramn=valuen]"
# - .e.g. configparse sitebleh.tmpl DOCROOT=/data/moo USER=ano HOSTNAME=scrolls.org
if [ ! "$1" ]; then
echo $USAGE
exit 1
fi
if [ ! -f $1 ]; then
echo "No template file found"
exit 1
fi
TEMPLATE_DATA=$(cat $1)
SITENAME=$2
VARS=(${@:(2)})
for var in ${VARS[@]}
do
key=$(echo $var | awk -F = '{ print $1}')
value=$(echo $var | awk -F = '{ print $2}')
TEMPLATE_DATA=$(echo "$TEMPLATE_DATA" | sed -r 's/'$key'/'$value'/g')
done
echo "$TEMPLATE_DATA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment