Created
April 9, 2014 20:15
-
-
Save skeggse/10309915 to your computer and use it in GitHub Desktop.
recursive md5 implementation with md5sum fallback, bash only
This file contains hidden or 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
#!/bin/bash | |
if [ "$#" -eq 0 ]; then | |
md5sum "$@" | |
exit 0 | |
fi | |
recursive=0 | |
exclude=0 | |
sum=0 | |
excludes=() | |
root=() | |
skip=0 | |
for param in "$@"; do | |
if [ $skip -ne 1 ]; then | |
if [ $exclude -eq 1 ]; then | |
excludes+=('(' '-name' "$param" '-prune' ')' '-o') | |
exclude=0 | |
elif [ "${param,,}" == '-r' ]; then | |
if [ $recursive -eq 1 ]; then | |
echo "unknown option -r" 1>&2 | |
exit 1 | |
fi | |
recursive=1 | |
elif [ "${param,,}" == '-e' ]; then | |
exclude=1 | |
elif [ "${param,,}" == '-s' ]; then | |
sum=1 | |
elif [ "$param" == '--' ]; then | |
skip=1 | |
elif [ "${param:0:1}" == '-' ]; then | |
echo "unknown option $param" 1>&2 | |
exit 1 | |
else | |
# TODO: combine with outer else clause | |
root+=("$param") | |
fi | |
else | |
root+=("$param") | |
fi | |
done | |
if [ $exclude -eq 1 ]; then | |
echo "expected exclude string" 1>&2 | |
exit 1 | |
fi | |
if [ $recursive -eq 0 ]; then | |
md5sum "$@" | |
exit 0 | |
fi | |
if [ "${#root[@]}" -eq 0 ]; then | |
root+=('.') | |
fi | |
find "${root[@]}" "${excludes[@]}" -type d -o -print0 | xargs -0 md5sum | (if [[ "$sum" -eq 1 ]]; then md5sum | awk '{ print $1 }'; else cat; fi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment