1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#!/usr/bin/env bash
set -ueo pipefail
usage() { echo "Usage: checksum -t <sha1|sha256> [--] [PATH]"; }
misuse() { usage 1>&2; exit 2; }
kind=''
while test $# -gt 0; do
case "$1" in
--)
shift
break
;;
-t)
shift
test $# -gt 0 || misuse
kind="$1"
case "$kind" in
sha1|sha256) ;;
*) misuse ;;
esac
shift
;;
-*)
misuse ;;
*)
break ;;
esac
done
test "$kind" || misuse
src=''
case $# in
0) ;;
1) src="$1" ;;
*) misuse ;;
esac
# Use KINDsum if available, else KIND (e.g. sha1sum or sha1). Assumes
# the former is compatible with the coreutils version, and the latter
# is compatible with the FreeBSD version.
if command -v "$kind"sum > /dev/null; then
if test "$src"; then
result=$("$kind"sum "$src")
else
result=$("$kind"sum)
fi
echo "${result%% *}"
elif command -v "$kind" > /dev/null; then
if test "$src"; then
"$kind" -q "$src"
else
"$kind" -q
fi
else
echo "Can't find sha1sum or sha1" 1>&2
exit 2
fi
|