forked from NMAAHC/nmaahcmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makechecksum
executable file
·63 lines (56 loc) · 3.42 KB
/
makechecksum
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
#!/usr/bin/env bash
#a script to create md5 checksums of all files in a directory or a single file and save it to a .md5 file
# load nmaahcmmfunctions into this script
SCRIPT_PATH="${0%/*}"
. "${SCRIPT_PATH}/nmaahcmmfunctions"
[[ -f "${SCRIPT_PATH}/nmaahcmmfunctions" ]] || { echo "Missing '${SCRIPT_PATH}/nmaahcmmfunctions'. Exiting." ; exit 1 ;};
_initialize_make # safe script termination process defined in nmaahcmmfunctions
unset DEPENDENCIES
DEPENDENCIES=(md5deep cowsay) # list dependencies required by script
_check_dependencies "${DEPENDENCIES[@]}" # defined in nmaahcmmfunctions
DATE=$(date '+%Y%m%d-%H:%M:%S')
### USAGE
_usage(){
echo
echo "$(basename "${0}")"
echo "This application creates md5 checksums. If you pass a single file as input, the application will create a checksum for that file and save it in a .md5 file in the same directory as your input. If you pass a directory as input, the application will create a checksum for every file in the directory and save them in .md5 files (one .md5 file for each original file) in the same directory that you supplied."
echo "Usage: $(basename ${0}) fileorpackage1 fileorpackage2 [fileorpackage3...]"
echo
exit
}
[ "${#}" = 0 ] && _usage # if the command is run with no arguments then _usage is called
# getopts loop
OPTIND=1
while getopts ":h" OPT; do
case "${OPT}" in
h) _usage ;; # if the operator runs "[scriptname] -h" then the _usage text above will display in the terminal
*) echo "Invalid option -${OPTARG}" ; _usage ;; # if the operator tries to use an option other than the ones listed above, the _usage text will display in the terminal
esac
done
shift $(( ${OPTIND} - 1 ))
## SCRIPT ACTIONS
_log -b
while [ "${*}" != "" ] ; do
INPUT="${1}" # name $INPUT as the first argument sent to script
if [[ -d "${INPUT}" ]] ; then # if argument is a directory, run md5deep
"${SCRIPTDIR}/removeDSStore" "${INPUT}"
TARGET="$(basename "${INPUT}")" # get basename of input, or ${INPUT##*/} - this command also strips the trailing slash, or ${INPUT%/}
OUTPUT="${INPUT}/${TARGET}_${DATE}_checksums.md5" # set .md5 file as $OUTPUT
_report -g "Making checksums of all files in directory ${TARGET} and writing to ${OUTPUT}"
md5deep -bre "${INPUT}" > "${OUTPUT}" # create md5 hash (hashes) of $INPUT and write results to $OUTPUT. -b=strip leading directory info, -r=recursive, -e=display progress indicator
fi
if [[ -f "${INPUT}" ]] ; then # if argument is a file, run md5deep
TARGET="$(basename "${INPUT}")" # get basename of input, or ${INPUT##*/}
OUTPUT_DIR="$(dirname "${INPUT}")" # get full path of parent directory, or ${INPUT%/*}
OUTPUT="${OUTPUT_DIR}/${TARGET%.*}_$(date +%F)_checksums.md5" # set .md5 file as $OUTPUT. ${TARGET%.*} strips extension from $TARGET
_report -g "Making checksum of file ${TARGET} and writing to ${OUTPUT}"
md5deep -bre "${INPUT}" > "${OUTPUT}" # create md5 hash of $INPUT and write results to $OUTPUT. -b=strip leading directory info, -r=recursive, -e=display progress indicator
fi
_report -g "Sorting checksums in $(basename "${OUTPUT}")"
sort -k 2 -o "${OUTPUT}" "${OUTPUT}" # sort $OUTPUT. -k 2=sort on the second field, -o=write OUTPUT to file (instead of standard terminal OUTPUT). The output is rewritten to the same file as previously, just sorted.
shift
done
# log script ending
_log -e
_report -g "makechecksum process complete."
exit $?