accept multiple filesnames to md5sum-update

This commit is contained in:
Ville Rantanen
2020-12-19 10:39:35 +02:00
parent b49c324524
commit a671c595c0

View File

@@ -2,57 +2,82 @@
function helpexit() {
echo 'Create or append md5sum list of files
Usage: md5sum-update [-r] [list name]
Usage: md5sum-update [-r] [-f list_name] [[files to check]]
-r recursive
list name is md5sums.txt by default
-f list_name is md5sums.txt by default
If files to check omitted, list files in current folder
Check files with "md5sum -c m5sums.txt"
Check files with "md5sum -c md5sums.txt"
To update hashes, simply delete the md5sums file.
'
exit
}
recursive=0
list_name="md5sums.txt"
paths=()
for ((i=1; i<=${#@}; i++)) {
j=$(( i + 1 ))
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-r" ]] && { recursive=1; continue; }
list_name="${!i}"
[[ "${!i}" = "-f" ]] && { list_name="${!j}"; i=$(( $i + 1 )); continue; }
paths+=("${!i}")
}
if [[ ${#paths[@]} -eq 0 ]]; then
paths+=(".")
fi
set -e
echo "Updating: $list_name"
test -f "$list_name" || touch "$list_name"
updates_done=0
_update() {
if [ "$file" = "$list_name" ]; then
if [ "$1" = "$list_name" ]; then
return
fi
if test -d "$file"; then
echo "$file is a directory"
if [[ -L "$1" ]]; then
echo "$1 is a symlink"
return
fi
if test -f "$file"; then
if grep -qF " $file" "$list_name"; then
echo "$file: exist"
if test -d "$1"; then
echo "$1 is a directory"
return
fi
if test -f "$1"; then
if awk '{ print substr($0,35) }' "$list_name" | grep -qFx "$1"; then
echo "$1: exist"
else
echo "$file: new"
md5sum "$file" >> "$list_name"
echo "$1: new"
md5sum "$1" >> "$list_name"
updates_done=1
fi
fi
}
shopt -s globstar
shopt -s nullglob
for ((i=0; i<=${#paths[@]}; i++)) {
if [[ -d "${paths[$i]}" ]]; then
pathname="${paths[$i]}"
pathname="${pathname%%/}"
if [[ "$recursive" -eq 1 ]]; then
for file in **; do
_update
for file in "${pathname}"/**; do
file="${file#./}"
_update "$file"
done
else
for file in *; do
_update
for file in "${pathname}"/*; do
file="${file#./}"
_update "$file"
done
fi
fi
if [[ -f "${paths[$i]}" ]]; then
file="${paths[$i]}"
file="${file#./}"
_update "$file"
fi
}
if [[ $updates_done -eq 1 ]]; then
sort -k 2 -o "$list_name" "$list_name"
fi