bash : refactor some scripts.

This commit is contained in:
Dmitry Voronin 2023-10-29 21:26:22 +03:00
parent 644b335458
commit 40774fd82e
3 changed files with 156 additions and 153 deletions

View file

@ -2,29 +2,29 @@
archive()
{
local target="$@" # target file(s).
local count=1 # processed count.
local count=0 # processed count.
local total=$# # total to process.
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
if [[ "${target}" = "" ]]; then
target="*"
total=$(ls | wc -l)
fi
# iterate each file.
for file in $target; do
for file in ${target}; do
# increment counter.
((count++))
# status info.
local status="[$count/$total] $file"
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
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
}
@ -32,29 +32,29 @@ archive()
archive_fast()
{
local target="$@" # target file(s).
local count=1 # processed count.
local count=0 # processed count.
local total=$# # total to process.
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
if [[ "${target}" = "" ]]; then
target="*"
total=$(ls | wc -l)
fi
# iterate each file.
for file in $target; do
for file in ${target}; do
# increment counter.
((count++))
# status info.
local status="[$count/$total] $file"
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
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
}
@ -62,29 +62,30 @@ archive_fast()
archive_check()
{
local target="$@" # target file(s).
local saved # saved hash value.
local actual # actual file hash value.
local count=1 # processed count.
local count=0 # processed count.
local total=$# # total to process.
local failed=0 # total failed checks.
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
if [[ "${target}" = "" ]]; then
target="*_*.tar.*"
total=$(ls ${target} | wc -l)
fi
# iterate each file.
for file in $target; do
for file in ${target}; do
# increment counter.
((count++))
# status info.
local status="[$count/$total] $file"
local status="[${count}/${total}] ${file}"
# extract hash from name.
saved="${file##*_}"
local saved="${file##*_}"
saved="${saved%%.*}"
# calculate actual hash.
actual="$(pv ${file} | sha1sum | cut -d\ -f1)"
local actual="$(pv ${file} | sha1sum | cut -d\ -f1)"
# compare hashes, show error on mismatch.
if [[ "${actual}" = "${saved}" ]]; then
@ -93,16 +94,15 @@ archive_check()
echo -e "${color_red}${status}: failed.${color_default}"
((failed++))
fi
# increment counter.
((count++))
done
# report result.
if [[ ${failed} -gt 0 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}"
if [[ ${count} -gt 1 ]]; then
if [[ ${failed} -gt 0 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}"
fi
fi
}
@ -110,39 +110,37 @@ archive_check()
unarchive()
{
local target="$@" # target file(s).
local saved # saved hash value.
local actual # actual file hash value.
local count=1 # processed count.
local count=0 # processed count.
local total=$# # total to process.
# set dafult value to target all supported archives.
if [[ "$target" = "" ]]; then
if [[ "${target}" = "" ]]; then
target="*_*.tar.*"
total=$(ls ${target} | wc -l)
fi
# iterate each file.
for file in $target; do
for file in ${target}; do
# increment counter.
((count++))
# status info.
local status="[$count/$total] $file"
local status="[${count}/${total}] ${file}"
# extract hash from name.
saved="${file##*_}"
local saved="${file##*_}"
saved="${saved%%.*}"
# calculate actual hash.
actual="$(sha1sum $file | cut -d\ -f1)"
local actual="$(sha1sum ${file} | cut -d\ -f1)"
# extract if hash matched or show error if not.
if [[ "$saved" = "$actual" ]]; then
if [[ "${saved}" = "${actual}" ]]; then
echo "${status}: OK."
tar -xf "$file"
tar -xf "${file}"
else
echo "${status}: failed."
fi
# increment counter.
((count++))
done
}
@ -174,7 +172,7 @@ archive_rename()
# export everything, primarily for use with parallel..
export -f archive archive_fast archive_check unarchive
# autocompletes.
# autocomplete.
_archive_list()
{
_autocomplete "$(ls *_*.tar.* 2> /dev/null)"

View file

@ -3,21 +3,22 @@
name()
{
local files="$@"
local count=1
local count=0
local total=$#
# all files except hidden by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls | wc -l)
fi
# process.
for file in $files; do
local new_name
for file in ${files}; do
# increment count.
((count++))
# extract new name.
new_name=$(echo "$file" | \
local new_name=$(echo "${file}" | \
sed -e "s/ /_/g" \
-e "s/[^[:alnum:]\._-]//g" \
-e "s/_\+/_/g" -e "s/\.\+/\./g" -e "s/-\+/-/g" \
@ -27,29 +28,25 @@ name()
)
# status line.
local status="[$count/$total] $file -> $new_name"
local status="[${count}/${total}] ${file} -> ${new_name}"
# check if same name.
if [[ "$file" = "$new_name" ]]; then
echo -e "${color_green}$status: no change.${color_default}"
((count++))
if [[ "${file}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: no change.${color_default}"
continue
fi
# check if file name already exists.
if [[ -f "$new_name" ]]; then
echo -e "${color_red}$status: already exists!${color_default}"
if [[ -f "${new_name}" ]]; then
echo -e "${color_red}${status}: already exists!${color_default}"
return 1
fi
# rename file.
mv -- "$file" "$new_name" &> /dev/null
mv -- "${file}" "${new_name}" &> /dev/null
# show change.
echo "$status"
# increment count.
((count++))
echo "${status}"
done
}
@ -58,45 +55,48 @@ name()
name_hash()
{
local files="$@"
local count=1
local count=0
local total=$#
# all files except hidden by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi
# process.
for file in $files; do
local extension
local new_name
for file in ${files}; do
# process only files.
if [[ -f "$file" ]]; then
# extract new name.
extension="${file##*.}"
if [ -f "$file" ] && [ "$extension" != "$file" ]; then
new_name="${file%$extension}"
else
new_name="$file"
if [[ -f "${file}" ]]; then
# increment count.
((count++))
# extract extension.
local extension="${file##*.}"
if [[ "${extension}" = "${file}" ]]; then
extension=""
fi
new_name=$(sha1sum -- "$file" | cut -d\ -f1)
if [[ "$extension" != "" ]]; then
new_name="${new_name,,}.$extension"
else
new_name="${new_name,,}"
extension=".${extension}"
fi
# hash the new name.
local hash=$(sha1sum -- "${file}" | cut -d\ -f1)
new_name="${hash,,}${extension}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# check if same name.
if [[ "${file}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: no change.${color_default}"
continue
fi
# rename file.
mv -- "$file" "$new_name" &> /dev/null
mv -- "${file}" "${new_name}" &> /dev/null
# show change.
echo "[$count/$total] $file -> $new_name"
# increment count.
((count++))
echo -e "${status}"
fi
done
}
@ -105,166 +105,170 @@ name_hash()
# usage: name_hash_check [FILES]
name_hash_check()
{
local stored
local actual
local files="$@"
local count=1
local count=0
local total=$#
local failed=0
# all files by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi
# process.
for file in $files; do
for file in ${files}; do
# process only files.
if [[ -f "$file" ]]; then
if [[ -f "${file}" ]]; then
# increment count.
((count++))
# status info.
local status="[$count/$total] $file"
local status="[${count}/${total}] ${file}"
# extract hashes.
stored="${file%%.*}"
actual=$(sha1sum -- "$file" | cut -d\ -f1)
local stored="${file%%.*}"
local actual=$(sha1sum -- "${file}" | cut -d\ -f1)
# compare hashes.
if [[ "$stored" = "$actual" ]]; then
if [[ "${stored}" = "${actual}" ]]; then
echo -e "${status}: OK."
else
echo -e "${color_red}${status}: failed.${color_default}"
((failed++))
fi
# increment count.
((count++))
fi
done
# report result.
if [[ ${failed} -gt 0 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}"
if [[ ${count} -gt 1 ]]; then
if [[ ${failed} -gt 0 ]]; then
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}"
fi
fi
}
# rename files for Jellyfin series, i.e. Episode S01E01.mkv
# usage: name_series SEASON
# usage: name_series [SEASON]
name_series()
{
local season="$1"
local season="${1}"
local files="${@:2}"
local count=1
local count=0
local total=$#
# error when no season number specified.
if [[ "$season" = "" ]]; then
echo "usage: name_series SEASON"
if [[ "${season}" = "" ]]; then
echo "usage: name_series [SEASON]"
return 1
fi
# all files by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi
# process.
for file in $files; do
local new_name
for file in ${files}; do
# process only files.
if [[ -f "$file" ]]; then
# extract new name.
new_name="Episode S${season}E$(printf %02d $count).${file##*.}"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
echo "$file -> $new_name"
if [[ -f "${file}" ]]; then
# increment episode number.
((count++))
# extract new name.
local new_name="Episode S${season}E$(printf %02d ${count}).${file##*.}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
echo -e "${status}"
fi
done
}
# rename files for Kavita manga format.
# usage: name_manga SEASON
# usage: name_manga [SEASON]
name_manga()
{
local season="$1"
local files="${@:2}"
local count=1
local count=0
local total=$#
local manga="${PWD##*/}"
# error when no season number specified.
if [[ "$season" = "" ]]; then
echo "usage: name_manga SEASON"
if [[ "${season}" = "" ]]; then
echo "usage: name_manga [SEASON]"
return 1
fi
# all files by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi
# process.
for file in $files; do
local new_name
for file in ${files}; do
# process only files.
if [[ -f "$file" ]]; then
# extract new name.
new_name="${manga} Vol.${season} Ch.${count}.${file##*.}"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
echo "$file -> $new_name"
if [[ -f "${file}" ]]; then
# increment episode number.
((count++))
# extract new name.
local new_name="${manga} Vol.${season} Ch.${count}.${file##*.}"
# prepare status.
local status="[${count}/${total}] ${file} -> ${new_name}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
echo -e "${status}"
fi
done
}
# rename files for new extension.
# usage: name_ext EXTENSION [FILES]
# usage: name_ext [EXTENSION] [FILES]
name_ext()
{
local extension="$1"
local files="${@:2}"
local count=1
local count=0
local total=$#
# error when no new extension specified.
if [[ "$extension" = "" ]]; then
echo "usage: name_ext EXTENSION [FILES]"
if [[ "${extension}" = "" ]]; then
echo "usage: name_ext [EXTENSION] [FILES]"
return 1
fi
# all files by default.
if [[ "$files" = "" ]]; then
if [[ "${files}" = "" ]]; then
files="[^.]*"
total=$(ls -p | grep -v / | wc -l)
fi
# process.
for file in $files; do
local new_name
for file in ${files}; do
# process only files.
if [[ -f "$file" ]]; then
# extract new name.
new_name="${file%.*}"."$extension"
# rename file.
mv -- "$file" "$new_name" &> /dev/null
# show change.
echo "[$count/$total] $file -> $new_name"
if [[ -f "${file}" ]]; then
# increment count.
((count++))
# extract new name.
local new_name="${file%.*}"."${extension}"
# rename file.
mv -- "${file}" "${new_name}" &> /dev/null
# show change.
echo "[${count}/${total}] ${file} -> ${new_name}"
fi
done
}

View file

@ -2,9 +2,10 @@
# usage: recursive [COMMAND]
recursive()
{
local IFS=$'\n'
local current="${PWD}"
local dirs=$(find -type d)
local total=$(echo ${dirs} | wc -w)
local total=$(find -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0
for dir in ${dirs}; do