Skip to content

Instantly share code, notes, and snippets.

@hasssan
Forked from Elemecca/addlicense.sh
Last active September 12, 2018 09:38
Show Gist options
  • Save hasssan/3d977514bdf904c5a267f53633d7e8bf to your computer and use it in GitHub Desktop.
Save hasssan/3d977514bdf904c5a267f53633d7e8bf to your computer and use it in GitHub Desktop.
Prepends a license notice to a set of files.
#!/bin/bash
#
# addlicense.sh - prepends a license notice to a set of files
#
# Originally written by Sam Hanes <sam@maltera.com>.
# To the extent possible under law, the author has waived all copyright
# and related or neighboring rights in this work, which was originally
# published in the United States. Attribution is appreciated but not
# required. The complete legal text of the release is available at
# http://creativecommons.org/publicdomain/zero/1.0/
if [[ $# -lt 3 ]]; then
echo "usage: addlicense.sh <license file> <target dir> <extension...>" >&2
exit 1
fi
license=$1
target=$2
ext=$3
shift; shift
if [[ ! -f "$license" ]]; then
echo "license file '$license' is not a regular file" >&2
exit 1
fi
if [[ ! -d "$target" ]]; then
echo "target directory '$target' is not a directory" >&2
exit 1
fi
for file in `find -L "$target" \( -type f -a -name "$ext" \) -print`; do
echo $file
cat "$license" "$file" > /tmp/licenseupdate
if [[ $? -ne 0 ]]; then
echo "failed to concatenate file '$file'" >&2
continue
fi
mv /tmp/licenseupdate $file
if [[ $? -ne 0 ]]; then
echo "failed to replace file '$file'" >&2
continue
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment