Created
June 13, 2014 08:58
-
-
Save andrioid/f5e7a6f20d948b0f273a to your computer and use it in GitHub Desktop.
Simple search/replace config file script in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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