Skip to content

Instantly share code, notes, and snippets.

@DonMartin76
Last active March 2, 2017 15:55
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 DonMartin76/cde753ec188b92f526e1a8d1c9b5caae to your computer and use it in GitHub Desktop.
Save DonMartin76/cde753ec188b92f526e1a8d1c9b5caae to your computer and use it in GitHub Desktop.
Quick, ugly, efficient bash script for templating files - takes everything ending with .template and replaces env vars inside them
#!/bin/bash
for tmpl in $(find . | grep '\.template'); do
targetFile=${tmpl%.*}
echo "Templating ${tmpl} to ${targetFile}..."
perl -pe 's;(\\*)(\$([a-zA-Z_][a-zA-Z_0-9]*)|\$\{([a-zA-Z_][a-zA-Z_0-9]*)\})?;substr($1,0,int(length($1)/2)).($2&&length($1)%2?$2:$ENV{$3||$4});eg' $tmpl > $targetFile
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment