-
Notifications
You must be signed in to change notification settings - Fork 3
/
dshasum
133 lines (121 loc) · 3.34 KB
/
dshasum
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash
#
# dshasum
# approach to caculate a folder/directory SHA checksums
#
#
# Usage info
show_help() {
cat << EOF
Directory SHA checksum (dshasum): Approach to caculate a folder/directory SHA checksums
Tony Liu, 2018-19 version 1.0.1
Usage: $(basename $0) -[h|?|v] [-f algorithm] [-p algorithm] [-t cheksum] path ...
path A full path of a directory
-p algorithm Path checksum algorithm, default=1
Valide shasum algorithm option, 1(default), 224, 256, 384, 512, 512224, 512256
-f algorithm files inside path checksum algorithm
default: equal to -p option
-h|-? Show this help
-v Verbose mode, which is good for debuging
-t [not implemented in this version]
Examples:
$(basename $0) -p 256 /var/root /bin
$(basename $0) -p 256 -f 1 /var/root
$(basename $0) -vp 224 /var/root
EOF
}
printInfo()
{
[[ $verbose -eq 1 ]] && echo "$1"
}
#
# allFileShasum()
# Get checksum of all files in a directory
# Input: <Directory>, <Algorithm>
# Output: formatted file shasum strings in order
# Return:
# 0 = Caculate folder OK
# 1 = Caculate one file OK
# 251 = Not a folder or file, or no access to it
# 252 = access Folder error
# 253 = access file error
#
allFileShasum()
{
path="$1"
opt="$2"
[[ -z "${opt// }" ]] && opt="1"
# -----------
if [[ -f "$path" ]]; then
if sum=$(/usr/bin/shasum -a $opt "$each"); then
echo "$sum"
return 1
else
return 253
fi
fi
# -----------
[[ -d "$path" ]] || return 251;
curPath=$(pwd)
cd "$path" || return 252
filelist=" - [Directory-Hash]"
IFS=$'\n'
NL=$'\n'
for each in $(/usr/bin/find -xd * -type f -not -name ".DS_Store" | sort); do
if sum=$(/usr/bin/shasum -a $opt "$each"); then
filelist="$filelist${NL}- $sum"
else
return 253
fi
done
echo "$filelist"
cd "$curPath"
}
#
# pathShasum()
# Caculate shasum base on the $1 string
# Input: <String> <Algorithm>
# Output: shasum string
# Return: 0
#
pathShasum()
{
opt="$2"
[[ -z "${opt// }" ]] && opt="1"
echo -n "$1" | /usr/bin/shasum -a $opt | /usr/bin/awk '{print $1}'
return 0
}
# Initialize our own variables:
verbose=0
path=""
pathAlg="1"
fileAlg=""
#-------------------------------------------------------------
# read the options
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "h?vp:f:t:" opt; do
case "$opt" in
h|\?) show_help; exit 1 ;;
v) verbose=1 ;;
p) pathAlg="$OPTARG" ;;
f) fileAlg="$OPTARG" ;;
t) checkString="$OPTARG" ;;
esac
done
[ -z "$fileAlg" ] && fileAlg="$pathAlg"
#-------------------------------------------------------------
# process the rest Path ... arguments
shift $((OPTIND-1)); [ "${1:-}" = "--" ] && shift
printInfo "Verbose=$verbose, Path Algorithm=$pathAlg, File Algorithm=$fileAlg, CheckString=$checkString, Path=$*"
while [[ $# -gt 0 ]]; do
eachPath="$1"
printInfo "Start process path=$1"
[ -d "$eachPath" ] || { echo "Error: access $eachPath"; exit 250; }
check=$(allFileShasum "$eachPath" "$fileAlg")
[[ "$?" != "0" ]] && { echo "Error: file shasum return $?."; exit 250; }
printInfo "$check"
check=$(pathShasum "$check" "$pathAlg")
echo "$check, $eachPath"
shift
done
exit 0