-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchecksum.sh
executable file
·49 lines (46 loc) · 1.15 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
#!/usr/bin/env bash
# Location: https://github.com/chainlayer/quicksync-playbooks/blob/master/roles/quicksync/files/checksum.sh
FILE=$1
COMMAND=$2
if [ "$FILE" == "" ]
then
echo "No file provided"
exit 1
fi
if [ "$COMMAND" != "check" -a "$COMMAND" != "create" ]
then
echo "no create or check command specified, assuming check"
COMMAND=check
fi
if [ ! -f ${FILE} ]
then
echo "no file found to checksum"
exit 1
fi
SIZE=`du -Bg $FILE|sed 's/\(\d*\)G.*/\1/'`
if [ "$COMMAND" == "create" ]
then
> ${FILE}.checksum
for((i=1;i<=$SIZE;++i)) do
dd bs=1M skip=$((1024*$i)) count=1 if=$FILE 2>/dev/null | sha512sum >> ${FILE}.checksum
done
echo "CHECKSUM CREATED"
exit 0
elif [ -f ${FILE}.checksum -a "$COMMAND" == "check" ]
then
for((i=1;i<=$SIZE;++i)) do
CHECKSUM=`dd bs=1M skip=$((1024*$i)) count=1 if=$FILE 2>/dev/null | sha512sum |awk '{print $1}'`
LINE=`grep -n $CHECKSUM ${FILE}.checksum|awk -F\: '{print $1}'`
if [ "$LINE" != "$i" ]
then
echo "CHECKSUM FAILED"
exit 1
fi
done
echo "CHECKSUM SUCCEEDED"
exit 0
elif [ ! -f ${FILE}.checksum -a "$COMMAND" == "check" ]
then
echo "no checksum file found"
exit 1
fi