Created
April 26, 2012 15:17
-
-
Save emmanuelbernard/2500305 to your computer and use it in GitHub Desktop.
Expand properties 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 | |
# | |
# Released under the WTFPL license version 2 http://sam.zoy.org/wtfpl/ | |
# Copyright (c) 2012 Emmanuel Bernard | |
# ----- | |
# Usage: properties-expander.sh <properties file> <template> <destination> | |
# | |
# Properties file: | |
# PROPERTY1=value1 | |
# PROPERTY2=value2 | |
#help | |
if [[ $# -ne 3 ]]; then | |
echo "properties-expander.sh <properties file> <template> <destination>" | |
exit 0; | |
fi | |
echo Expanding $2 to $3 with properties from $1 | |
#read each line including the last one | |
while IFS=$'\n' read -r line || [[ -n "$line" ]]; do | |
#get the property name before the first = | |
property=${line%%=*} | |
#get the value after the first = | |
value=${line#*=} | |
#find a suitable separation character as the value might contain / | |
candidates='/:.|@#%^&;,!~abcABC' # candidates for separation character | |
char=$(echo "$candidates" | tr -d "$line") | |
char=${char:0:1} # choose the first candidate that doesn't appear in the user input | |
if [ -z "$char" ] # this test checks for exhaustion of the candidate character set | |
then | |
echo "Unusable user input: $line" | |
exit 1 | |
fi | |
#add regexp rule | |
SEDEXEC="s${char}\$$property${char}$value${char}g;$SEDEXEC" | |
done < $1 | |
SEDEXEC="sed -e '$SEDEXEC' $2 > $3" | |
eval $SEDEXEC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment