Skip to content

Instantly share code, notes, and snippets.

@netj
Created June 13, 2010 11:21
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 netj/436583 to your computer and use it in GitHub Desktop.
Save netj/436583 to your computer and use it in GitHub Desktop.
Substitute contents and names of given files
#!/usr/bin/env bash
# customize -- Substitute contents and names of given files
#
# Usage:
# customize DEST [FILE...] <<RULES
# Name=Foo
# Version=1.0
# RULES
#
#
# Author: Jaeho Shin <netj@sparcs.org>
# Created: 2009-12-18
set -e
Dest=$1; shift || { sed -n "2,/^#$/s/^# //p" <"$0"; exit; }
[ $# -gt 0 ] || set -- *
# prepare substitutions
subst=`sed 's/:/\\\\:/g; s/^/s:/; s/=/:/; s/$/:g/'`
# customize by substituting everything here
find "$@" -type f |
while read -r f; do
[ -e "$f" ] || continue
# customize name
g=`sed "$subst" <<<"$f"`
h="$Dest/$g"
d=`dirname "$h"`
[ -d "$d" ] || mkdir -p "$d"
# customize contents
sed "$subst" <"$f" >"$h"
! [ -x "$f" ] || chmod +x "$h"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment