Skip to content

Commit 3ad3a06

Browse files
committed
Add support for providing multiple files and checksums
1 parent 70f86fe commit 3ad3a06

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

tools/scripts/checksum

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22
#
3-
# Verify the checksum of a file.
3+
# Verify the checksum of one or more files.
44
#
5-
# Usage: checksum <filepath> <hash>
5+
# Usage: checksum <filepath> <hash> <filepath> <hash> ...
66
#
77
# Arguments:
88
#
@@ -18,25 +18,30 @@ checksum_type='sha256'
1818
# Program used to compute a checksum:
1919
checksum_program=
2020

21-
# File to verify:
22-
file=
23-
2421
# Command to compute a checksum:
2522
cmd=
2623

27-
# Expected checksum:
28-
expected_checksum=
29-
30-
# Actual checksum:
31-
curr_checksum=
32-
3324

3425
# FUNCTIONS #
3526

3627
# Prints usage information.
3728
usage() {
3829
echo '' >&2
39-
echo 'Usage: checksum <filepath> <hash>' >&2
30+
echo 'Usage: checksum <filepath> <hash> <filepath> <hash> ...' >&2
31+
echo '' >&2
32+
}
33+
34+
# Defines an error handler.
35+
#
36+
# $1 - error status
37+
on_error() {
38+
echo 'ERROR: An error was encountered during execution.' >&2
39+
cleanup
40+
exit "$1"
41+
}
42+
43+
# Runs clean-up tasks.
44+
cleanup() {
4045
echo '' >&2
4146
}
4247

@@ -91,37 +96,59 @@ print_checksum_error() {
9196
echo '' >&2
9297
}
9398

99+
# Verifies a checksum.
100+
#
101+
# $1 - file
102+
# $2 - expected checksum
103+
verify() {
104+
local curr_checksum=$(eval "${cmd} $1 | awk '{ print \$1; }'")
105+
106+
if [[ "${curr_checksum}" != "$2" ]]; then
107+
print_checksum_error "$1" "${checksum_type}" "${checksum_program}" "$2" "${curr_checksum}"
108+
return 1
109+
fi
110+
return 0
111+
}
112+
113+
# Main execution sequence.
114+
main() {
115+
local args
116+
local len
117+
118+
args=("$@")
119+
len="${#args[@]}"
120+
121+
for (( i=0; i<${len}; i+=2 )) ; do
122+
verify "${args[i]}" "${args[i+1]}"
123+
if [[ "$?" -ne 0 ]]; then
124+
on_error 1
125+
fi
126+
done
127+
print_success
128+
cleanup
129+
exit 0
130+
}
131+
94132

95133
# MAIN #
96134

97135
# Handle arguments...
98136
if [[ "$#" -eq 0 ]]; then
99137
usage
100138
exit 0
101-
elif [[ "$#" -eq 2 ]]; then
102-
file="$1"
103-
expected_checksum="$2"
104-
else
105-
echo 'ERROR: unrecognized arguments. Must provide a file to verify and an expected checksum.' >&2
139+
elif [[ $(($#%2)) -eq 1 ]]; then
140+
echo 'ERROR: invalid arguments. Each file to verify must be followed by an expected checksum.' >&2
106141
exit 1
107142
fi
108143

109144
# Find a program for computing a SHA256 checksum...
110145
if [[ -n "$(which sha256sum)" ]]; then
111146
checksum_program="sha256sum"
112-
cmd="sha256sum ${file} | awk '{ print \$1; }'"
147+
cmd='sha256sum'
113148
elif [[ -n "$(which shasum)" ]]; then
114149
checksum_program="shasum"
115-
cmd="shasum -a 256 ${file} | awk '{ print \$1; }'"
150+
cmd='shasum -a 256'
116151
fi
117152

118-
# Verify checksum:
119-
curr_checksum=$(eval "${cmd}")
120-
121-
if [[ "${curr_checksum}" != "${expected_checksum}" ]]; then
122-
print_checksum_error "${file}" "${checksum_type}" "${checksum_program}" "${expected_checksum}" "${curr_checksum}"
123-
exit 1
124-
else
125-
print_success
126-
exit 0
127-
fi
153+
# Run main:
154+
main "$@"

0 commit comments

Comments
 (0)