-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-checksum.sh
More file actions
171 lines (127 loc) · 5.01 KB
/
Copy pathverify-checksum.sh
File metadata and controls
171 lines (127 loc) · 5.01 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/ksh
# From Crystal Kolipe
# https://marc.info/?l=openbsd-misc&m=175225722717854
# If the first argument is "i", create an empty checksums file
if [ "$1" == "i" ]; then
touch checksums
fi
# Loop through all files named "checksums" found in the current
# directory and subdirectories
for i in `find . -name checksums`; do
(
# Print verification status based on the first argument
if [ "$1" == "a" ]; then
echo -n "Not v"
else
echo -n "V"
fi
echo "erifying checksums in directory ${i%/checksums}"
# Change to the directory containing the checksums file
cd "${i%/checksums}"
# If not in "add" mode, verify checksums using sha512
if [ "$1" != "a" ]; then
sha512 -cq checksums
fi
# Initialize a flag to track missing entries
let flag=0
# Loop through all files except checksums and checksums.bak
for j in !(checksums|checksums.bak); do
# If the item is a file (not a directory)
if [ ! -d "$j" ]; then
# Check if the file is listed in the checksums file
grep -F "($j)" checksums > /dev/null || {
if [ -z "$1" ]; then
# If no argument is given, warn about missing
# entry
echo "$j is not in the checksums file!"
let flag=1
else
# If any argument is given, add the file to
# checksums
echo "Adding $j to checksums file"
sha512 "$j" >> checksums
fi
}
fi
done
# Report whether all files are accounted for
if [ $flag -eq 1 ]; then
echo "Run $0 with any command line arguments to add missing"
echo "entries to the checksums file."
else
echo "All files have entries in the checksum file."
fi
)
done
# If "i" mode was used and the checksums file is still empty, remove it
if [ "$1" == "i" -a ! -s checksums ]; then
rm -f checksums
fi
if false; then
NAME
verify_checksums.sh - verify or initialize SHA-512 checksums for files
SYNOPSIS
verify_checksums.sh [MODE]
DESCRIPTION
This script recursively locates all files named 'checksums' in the current
directory tree and verifies or updates their contents using SHA-512 hashes.
It supports three modes of operation:
i Initialize mode. Creates a 'checksums' file in the current directory.
If no files are added, the file is deleted at the end.
a Add mode. Adds missing files to each 'checksums' file using SHA-512
hashes. Does not perform verification.
(no argument)
Verification mode. Verifies each file listed in the 'checksums' file
using SHA-512. Warns about any missing entries.
BEHAVIOR
- For each 'checksums' file found:
• Changes into its parent directory.
• If in verification mode, runs `sha512 -cq checksums`.
• Iterates over all files except 'checksums' and 'checksums.bak'.
• If a file is not listed in 'checksums':
- In verification mode, prints a warning.
- In add mode, appends its SHA-512 hash to 'checksums'.
- At the end of each directory scan:
• Reports whether all files are accounted for.
• Suggests rerunning in add mode if entries are missing.
- In initialize mode:
• Creates a 'checksums' file.
• Deletes it if no entries are added.
REQUIREMENTS
- sha512 command must be available in PATH.
- Shell must support extended globbing (e.g., Bash with `shopt -s extglob`).
EXAMPLES
Initialize a new checksums file:
./verify_checksums.sh i
Add missing entries to all checksums files:
./verify_checksums.sh a
Verify all checksums and report missing entries:
./verify_checksums.sh
AUTHOR
Written by [Your Name Here]
COPYRIGHT
This is free software; you may redistribute it under the terms of the MIT
License. See the LICENSE file for details.
Incompatible with OpenBSD sh
OpenBSD's sh is a strict POSIX shell and does not support:
Extended globbing:
sh
for j in !(checksums|checksums.bak)
This syntax is Bash/ksh-specific. POSIX sh does not support !(pattern).
== string comparison:
sh
if [ "$1" == "i" ]
POSIX sh uses = for string comparison. == is a Bash/ksh extension.
let arithmetic:
sh
let flag=0
POSIX sh uses flag=0 and flag=$((flag + 1)) for arithmetic. let is not available.
Subshell grouping with (...) inside for loop: While POSIX sh supports subshells, the way it's used here may behave differently depending on the shell.
Compatible with ksh or bash
This script is compatible with:
KornShell (ksh) — especially on OpenBSD, which includes ksh as /bin/ksh.
Bash — if installed separately.
How to run it on OpenBSD
To run this script successfully on OpenBSD, use:
sh
fi