Skip to content

Instantly share code, notes, and snippets.

@stefco
Created July 15, 2016 16:33
Show Gist options
  • Save stefco/72fad1653573315219586d0d8efaad6f to your computer and use it in GitHub Desktop.
Save stefco/72fad1653573315219586d0d8efaad6f to your computer and use it in GitHub Desktop.
Make a new website from a template in some folder, and replacing a placeholder string with the customer's name
#!/bin/bash
#------------------------------------------------------------------------------
# HELP INFO
#------------------------------------------------------------------------------
#
# example usage: make a new web site in a folder called "new_site" for someone
# named Rachel Bronstein:
#
# newblanksite new_site 'Rachel Bronstein'
#
# if you want to make this file run on your computer, you should edit the part
# below where the ~/path/to/template is written. you will also need to change
# to the directory where you save this file and run
#
# chmod +x newblanksite
#
# in order to make it executable. if you want to be fancy and make it possible
# to run this from anywhere on your mac, you can e.g. put it in a folder
# and put that folder on your PATH, the variable bash uses when it looks for
# available commands:
#
# mkdir ~/scripts
# mv newblanksite ~/scripts
# chmod +x ~/scripts/newblanksite
# export PATH=~/scripts:"$PATH"
#
# if you want it to always be in your PATH, you can put that last command in
# your ~/.bash_profile (mac) or ~/.bashrc (linux) script; when you start a
# new terminal session, your computer runs that startup script in order to
# configure things how you like them. you can put that last command in your
# .bash_profile with this command:
#
# echo 'export PATH=~/scripts:"$PATH"' >> ~/.bash_profile
#
# to reload your startup script (and apply the change you just made),
# you can always run:
#
# source ~/.bash_profile
#
# note that you can always use the `source` command to run some code
# that is stored in a script. there is nothing special about
# .bash_profile except that your shell automatically `source`s it
# when you start up a session.
#------------------------------------------------------------------------------
# THE ACTUAL CODE
#------------------------------------------------------------------------------
#
# the next 2 lines will read 2 arguments from the command line and assume
# the 1st is the path to your new website folder and the second is
dir="$1"
name="$2"
# the next line picks the unique string that we will replace in our template
# pages
name_template="{{name}}"
# copy the directory
cp -R ~/path/to/template "$dir"
# replace the name_template string in each file with the customer's name using
# sed. the -i flag means "in place"; sed will edit the file you specify. the
# blank string provided as the second argument specifies the file extension
# for a backup file, e.g. if it was '.original' then i would not save the
# original files with '.original' appended to the end in case my sed command
# fucked them up. in this case, i don't want a backup, since this is a simple
# substitution, and since it is easy to just fix the script and copy everything
# anew, so i specify a blank string for my extension. the next part says
# i should replace the name_template string out with the customer's name
# wherever i see it; the g at the end means do this for every match (by default
# this is only done once per line). you can, of course, change which files
# are edited by changing the file names at the end, and you can edit more files
# by just copying the command onto a new line and (again) changing the
# filenames. at some point you would probably want to use a for loop and
# arrays, but that is overkill for a few files.
sed -i '' "s/$name_template/$name/g" "$dir"/index.html
sed -i '' "s/$name_template/$name/g" "$dir"/profile/index.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment