Skip to content

Instantly share code, notes, and snippets.

@emaV
Created November 4, 2016 10:16
Show Gist options
  • Save emaV/7e01cf984c7a000e2702dc5b09459ab7 to your computer and use it in GitHub Desktop.
Save emaV/7e01cf984c7a000e2702dc5b09459ab7 to your computer and use it in GitHub Desktop.
twig template with bash
#!/bin/bash
# Process twig template for WordPress config
if [ $# -ne 1 ]; then
echo 1>&2 Usage: $0 domain
exit 127
fi
# Set variables
domain=$1
# Variables
dbName='zut'
dbUser='miao'
dbPassword='qwr'
dbHost='lollll'
dbTablePrefix='tab_'
wpHome="http://${domain}"
wpSiteUrl="http://${domain}"
# Regex
REGEX_dbName="s/{{\ dbName\ }}/${dbName}/g"
REGEX_dbUser="s/{{\ dbUser\ }}/${dbUser}/g"
REGEX_dbPassword="s/{{\ dbPassword\ }}/${dbPassword}/g"
REGEX_dbHost="s/{{\ dbHost\ }}/${dbHost}/g"
REGEX_dbTablePrefix="s/{{\ dbTablePrefix\ }}/${dbTablePrefix}/g"
# For variables with '/' use '#' as sed separator
REGEX_wpHome="s#{{\ wpHome\ }}#${wpHome}#g"
REGEX_wpSiteUrl="s#{{\ wpSiteUrl\ }}#${wpSiteUrl}#g"
echo $REGEX_dbTablePrefix
# Process template
$template='wp-config.php.twig'
$configFile="/var/www/${domain}/www/wp-config.php"
cat $template |
sed -e "${REGEX_dbName}" |
sed -e "${REGEX_dbUser}" |
sed -e "${REGEX_dbPassword}" |
sed -e "${REGEX_dbHost}" |
sed -e "${REGEX_dbTablePrefix}" |
sed -e "${REGEX_wpHome}" |
sed -e "${REGEX_wpSiteUrl}" > ${configFile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment