From fa4c85ade8274e8a81e3fb47439ef00530a4b11a Mon Sep 17 00:00:00 2001 From: desktop Date: Tue, 3 Oct 2023 06:50:40 +0300 Subject: [PATCH] bash : add progress counter to archive. --- document/linux/config/bash/module/archive.sh | 48 ++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/document/linux/config/bash/module/archive.sh b/document/linux/config/bash/module/archive.sh index 97b3b30..b881fe5 100644 --- a/document/linux/config/bash/module/archive.sh +++ b/document/linux/config/bash/module/archive.sh @@ -2,22 +2,29 @@ archive() { local target="$@" # target file(s). + local count=1 + local total=$# # set dafult value to target all supported archives. if [[ "$target" = "" ]]; then target="*" + total=$(ls | wc -l) fi # iterate each file. for file in $target; do - # print archive name. - echo "$file" + # status info. + local status="[$count/$total] $file" + echo "${status}" # create archive. tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | xz -9e > "${file%/*}".tar.xz # append hash to file name. mv "${file%/*}".tar.xz "${file%/*}"_$(sha1sum "${file%/*}".tar.xz | cut -d\ -f1).tar.xz + + # increment counter. + ((count++)) done } @@ -25,22 +32,29 @@ archive() archive_fast() { local target="$@" # target file(s). + local count=1 + local total=$# # set dafult value to target all supported archives. if [[ "$target" = "" ]]; then target="*" + total=$(ls | wc -l) fi # iterate each file. for file in $target; do - # print archive name. - echo "$file" + # status info. + local status="[$count/$total] $file" + echo "${status}" # create archive. tar -c "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | gzip -1 > "${file%/*}".tar.gz # append hash to file name. mv "${file%/*}".tar.gz "${file%/*}"_$(sha1sum "${file%/*}".tar.gz | cut -d\ -f1).tar.gz + + # increment counter. + ((count++)) done } @@ -50,14 +64,20 @@ archive_check() local target="$@" # target file(s). local saved # saved hash value. local actual # actual file hash value. + local count=1 + local total=$# # set dafult value to target all supported archives. if [[ "$target" = "" ]]; then target="*_*.tar.*" + total=$(ls ${target} | wc -l) fi # iterate each file. for file in $target; do + # status info. + local status="[$count/$total] $file" + # extract hash from name. saved="${file##*_}" saved="${saved%%.*}" @@ -67,10 +87,13 @@ archive_check() # compare hashes, show error on mismatch. if [[ "$actual" = "$saved" ]]; then - echo "$file: OK." + echo "${status}: OK." else - echo "$file: failed." + echo "${status}: failed." fi + + # increment counter. + ((count++)) done } @@ -80,14 +103,20 @@ unarchive() local target="$@" # target file(s). local saved # saved hash value. local actual # actual file hash value. + local count=1 + local total=$# # set dafult value to target all supported archives. if [[ "$target" = "" ]]; then target="*_*.tar.*" + total=$(ls ${target} | wc -l) fi # iterate each file. for file in $target; do + # status info. + local status="[$count/$total] $file" + # extract hash from name. saved="${file##*_}" saved="${saved%%.*}" @@ -97,11 +126,14 @@ unarchive() # extract if hash matched or show error if not. if [[ "$saved" = "$actual" ]]; then - echo "$file: OK." + echo "${status}: OK." tar -xf "$file" else - echo "$file: failed." + echo "${status}: failed." fi + + # increment counter. + ((count++)) done }