From a671c595c031f6948060aa2ae67775c6da35bd86 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Sat, 19 Dec 2020 10:39:35 +0200 Subject: [PATCH] accept multiple filesnames to md5sum-update --- files/md5sum-update | 89 +++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/files/md5sum-update b/files/md5sum-update index 9d4556d..f26d388 100755 --- a/files/md5sum-update +++ b/files/md5sum-update @@ -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 - return - fi - if test -d "$file"; then - echo "$file is a directory" - return - fi - if test -f "$file"; then - if grep -qF " $file" "$list_name"; then - echo "$file: exist" - else - echo "$file: new" - md5sum "$file" >> "$list_name" + if [ "$1" = "$list_name" ]; then + return + fi + if [[ -L "$1" ]]; then + echo "$1 is a symlink" + return + fi + 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 "$1: new" + md5sum "$1" >> "$list_name" + updates_done=1 + fi 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 "${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 - for file in **; do - _update - done -else - for file in *; do - _update - done +if [[ $updates_done -eq 1 ]]; then + sort -k 2 -o "$list_name" "$list_name" fi - - -sort -k 2 -o "$list_name" "$list_name"