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() { function helpexit() {
echo 'Create or append md5sum list of files 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 -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 exit
} }
recursive=0 recursive=0
list_name="md5sums.txt" list_name="md5sums.txt"
paths=()
for ((i=1; i<=${#@}; i++)) { for ((i=1; i<=${#@}; i++)) {
j=$(( i + 1 ))
[[ "${!i}" = "-h" ]] && helpexit [[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "--help" ]] && helpexit [[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-r" ]] && { recursive=1; continue; } [[ "${!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 set -e
echo "Updating: $list_name" echo "Updating: $list_name"
test -f "$list_name" || touch "$list_name" test -f "$list_name" || touch "$list_name"
updates_done=0
_update() { _update() {
if [ "$file" = "$list_name" ]; then if [ "$1" = "$list_name" ]; then
return return
fi fi
if test -d "$file"; then if [[ -L "$1" ]]; then
echo "$file is a directory" echo "$1 is a symlink"
return return
fi fi
if test -f "$file"; then if test -d "$1"; then
if grep -qF " $file" "$list_name"; then echo "$1 is a directory"
echo "$file: exist" return
fi
if test -f "$1"; then
if awk '{ print substr($0,35) }' "$list_name" | grep -qFx "$1"; then
echo "$1: exist"
else else
echo "$file: new" echo "$1: new"
md5sum "$file" >> "$list_name" md5sum "$1" >> "$list_name"
updates_done=1
fi fi
fi fi
} }
shopt -s globstar shopt -s globstar
shopt -s nullglob 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 "${pathname}"/**; do
file="${file#./}"
_update "$file"
done
else
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 [[ "$recursive" -eq 1 ]]; then if [[ $updates_done -eq 1 ]]; then
for file in **; do sort -k 2 -o "$list_name" "$list_name"
_update
done
else
for file in *; do
_update
done
fi fi
sort -k 2 -o "$list_name" "$list_name"