-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.sh
executable file
·68 lines (57 loc) · 1.37 KB
/
checksum.sh
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
63
64
65
66
67
#!/bin/bash
# sha 256 is default (recommended)
checksum_executable=sha256sum
HELP="Usage: $0 [-a checksum_executable] [FILE]..."
TEMP=`getopt -o a:h --long algorithm:,help -n "$0" -- "$@"`
if [ $? != 0 ] ; then echo "Abandon" >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--algorithm) checksum_executable=$2 ; shift 2 ;;
-h|--help) echo "$HELP"
exit 0 ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
if ! which $checksum_executable > /dev/null
then
echo "$checksum_executable was not found" >&2
exit 1
fi
if (( $# == 0 ))
then
$checksum_executable
else
if ! which realpath > /dev/null
then
echo "$0: realpath was not found" >&2
exit 1
else
IFS='
'
absolute_executable=$(realpath $0)
if [ "$absolute_executable" == "" ]
then
absolute_executable=$0
fi
for arg in $*
do
if test -d "$arg"
then
cd "$arg"
sum=$(for f in $(find . -maxdepth 1 -mindepth 1 | LANG=C sort)
do
$absolute_executable $f
done | $checksum_executable | cut -f 1 -d ' ')
printf "\033[2K" >&2
echo "$sum $arg"
cd - > /dev/null
else
printf "\033[2K%s\r" "$arg" >&2
$checksum_executable "$arg"
fi
done
fi
fi