Created
June 13, 2010 11:21
-
-
Save netj/436583 to your computer and use it in GitHub Desktop.
Substitute contents and names of given files
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
#!/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 <[email protected]> | |
# 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