bash : reduce output.

This commit is contained in:
Dmitry Voronin 2023-11-24 01:19:49 +03:00
parent a3d12ece74
commit aa50193fb7
6 changed files with 347 additions and 280 deletions

View file

@ -359,9 +359,8 @@ Command|Description
Command|Description Command|Description
---|--- ---|---
`checksum_create`|Create checksums for files in current directory. `checksum_create [FILES]`|Create checksums for files in current directory.
`checksum_check`|Check previously created checksums. `checksum_check [FILES]`|Check previously created checksums.
`checksum_update`|Check old hashes and create new ones for all the files.
## Chmod. ## Chmod.

View file

@ -11,9 +11,9 @@ archive()
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local failed=0
local total=${#} # total to process. local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp. local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
# Set dafult value to target all directories. # Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -31,18 +31,23 @@ archive()
echo -e "${status}" echo -e "${status}"
# create archive. # create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz || ((failed++)) tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz
# append hash to target name. # append hash to target name.
mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz || ((failed++)) mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz
# report success. # Show error.
if [[ ${failed} = 0 ]]; then if [[ ${?} != 0 ]]; then
echo -e "${color_green}${status}: Done.${color_default}" ((failed++))
else
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
fi fi
done done
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# archive file with minimal compression and checksum. # archive file with minimal compression and checksum.
@ -52,9 +57,9 @@ archive_fast()
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local failed=0
local total=${#} # total to process. local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp. local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
# Set dafult value to target all directories. # Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -72,18 +77,23 @@ archive_fast()
echo -e "${status}" echo -e "${status}"
# create archive. # create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz || ((failed++)) tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz
# append hash to target name. # append hash to target name.
mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz || ((failed++)) mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz
# report success. # Show error.
if [[ ${failed} = 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${color_green}${status}: Done.${color_default}" ((failed++))
else
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
fi fi
done done
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# check archive hashes. # check archive hashes.
@ -94,7 +104,7 @@ archive_check()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local total=${#} # total to process. local total=${#} # total to process.
local count=0 # processed count. local count=0 # processed count.
local failed=0 # total failed checks. local failed=0
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -104,14 +114,12 @@ archive_check()
# iterate each target. # iterate each target.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only files.
[[ -f "${target}" ]] || continue
# increment counter. # increment counter.
((count++)) ((count++))
# status info. # status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hash from name. # extract hash from name.
local saved="${target##*-}" local saved="${target##*-}"
@ -121,19 +129,15 @@ archive_check()
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1) local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# compare hashes, show error on mismatch. # compare hashes, show error on mismatch.
if [[ "${actual}" = "${saved}" ]]; then if [[ "${actual}" != "${saved}" ]]; then
echo -e "${status}: Validation OK."
else
echo -e "${color_bred}${status}: Validation failed.${color_default}"
((failed++)) ((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi fi
done done
# report result. if [[ ${failed} != 0 ]]; then
if [[ ${failed} -gt 0 ]]; then echo -e "${color_bred}Failed: ${failed}.${color_default}"
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}" false
else
echo -e "${color_green}All successful.${color_default}"
fi fi
} }
@ -144,8 +148,8 @@ archive_prune()
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local failed=0
local total=${#} local total=${#}
local failed=0
# All archives by default. # All archives by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -173,16 +177,19 @@ archive_prune()
if [[ "${copy_time}" -lt "${time}" ]]; then if [[ "${copy_time}" -lt "${time}" ]]; then
echo -e "${name}: prune ${copy_time}." echo -e "${name}: prune ${copy_time}."
rm -- "${copy}" || ((failed++)) rm -- "${copy}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
fi fi
done done
done done
# Report result. if [[ ${failed} != 0 ]]; then
if [[ ${failed} = 0 ]]; then echo -e "${color_bred}Failed: ${failed}.${color_default}"
echo -e "${color_green}All successful.${color_default}" false
else
echo -e "${color_bred}Items failed to prune: ${failed}.${color_default}"
fi fi
} }
@ -194,6 +201,7 @@ unarchive()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
local failed=0
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -208,6 +216,7 @@ unarchive()
# status info. # status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hash from name. # extract hash from name.
local saved="${target##*-}" local saved="${target##*-}"
@ -218,8 +227,6 @@ unarchive()
# extract if hash matched or show error if not. # extract if hash matched or show error if not.
if [[ "${saved}" = "${actual}" ]]; then if [[ "${saved}" = "${actual}" ]]; then
echo -e "${status}: Validation OK."
# figure out the compression tool. # figure out the compression tool.
local compressor local compressor
case "${target##*.}" in case "${target##*.}" in
@ -234,15 +241,23 @@ unarchive()
# extract. # extract.
unset IFS unset IFS
pv "${target}" | ${compressor} | tar -xf - pv "${target}" | ${compressor} | tar -xf -
else
# report validation error & exit.
echo -e "${color_bred}${status}: Validation failed.${color_default}"
return 1
fi
# report extraction complete. if [[ ${?} != 0 ]]; then
echo -e "${color_green}${status}: Done.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
else
# report validation error & continue.
echo -e "${color_bred}${status}: Validation failed.${color_default}"
((failed++))
continue
fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# rename archive. if no name specified, it simplifies archive's name. # rename archive. if no name specified, it simplifies archive's name.
@ -254,6 +269,7 @@ archive_name()
local name="${2}" # new name. local name="${2}" # new name.
local total=1 # total targets to process. local total=1 # total targets to process.
local count=0 # processed targets counter. local count=0 # processed targets counter.
local failed=0
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -263,9 +279,6 @@ archive_name()
# iterate each target. # iterate each target.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# only work with files.
[[ -f "${target}" ]] || continue
# iterate counter. # iterate counter.
((count++)) ((count++))
@ -284,19 +297,32 @@ archive_name()
# check for the same name. # check for the same name.
if [[ "${target}" = "${new_name}" ]]; then if [[ "${target}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: No change.${color_default}" echo -e "${status}"
continue continue
fi fi
# check for existing target. # check for existing target.
if [[ -f "${new_name}" ]]; then if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: Already exists.${color_default}" echo -e "${color_bred}${status}: Already exists.${color_default}"
return 1 ((failed++))
continue
fi fi
echo -e "${status}"
# rename. # rename.
mv -- "${target}" "${new_name}" && echo -e "${status}" || echo -e "${color_bred}${status}: Error.${color_default}" mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives. # convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives.

View file

@ -1,53 +1,111 @@
# Save file checksums. # Save file checksums.
# Usage: checksum_create [FILES]
checksum_create() checksum_create()
{ {
local file=".checksum" local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
# Error if checksum already exists. # All files by default.
if [[ -f "${file}" ]]; then if [[ "${targets}" = "" ]]; then
echo -e "${color_bred}Checksum already exists.${color_default}" targets=($(ls --classify | grep -v /\$))
return 1 total=${#targets[@]}
fi fi
find -type f | parallel -j $(_core_count) -- sha1sum {} >> "${file}" # Iterate each file.
sed -i "/${file}/d" ${file} for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Increment count.
((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash exists.
[[ -f "${hashfile}" ]] && continue
# Calculate hash.
pv "${target}" | sha1sum > ${hashfile}
# Report success.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# Check stored values against actual files. # Check stored values against actual files.
checksum_check() checksum_check()
{ {
local file=".checksum" local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0 local failed=0
local skipped=0
cat "${file}" | parallel -j $(_core_count) -- _checksum_check {} || ((failed++)) # All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
if [[ ${failed} = 0 ]]; then # Iterate each file.
echo -e "${color_green}All successful.${color_default}" for target in "${targets[@]}"; do
return 0 local hashfile=".${target#./}.sha1"
else
# echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}" # Increment count.
return 1 ((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash doesn't exist.
[[ -f "${hashfile}" ]] || { ((skipped++)); continue; }
# Calculate hash.
local stored=$(cat "${hashfile}" | cut -d\ -f1)
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi fi
} }
# Check old values and create new one if all was fine. _checksum_create()
checksum_update()
{ {
local file=".checksum" local path="${1%/*}"
local name="${1##*/}"
if [[ ! -f "${file}" ]]; then sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
echo -e "${color_bred}Checksum doesn't exist.${color_default}"
return 1
fi
checksum_check && rm "${file}" && checksum_create
} }
_checksum_check() _checksum_check()
{ {
local file="${1##*\ \ }" local file="${1##*\ \ }"
local stored="${1%%\ \ *}" local stored="${1%%\ \ *}"
local failed=0
# Skip if no file. # Skip if no file.
[[ -f "${file}" ]] || return 0 [[ -f "${file}" ]] || return 0
@ -65,4 +123,5 @@ _checksum_check()
return 0 return 0
} }
export -f _checksum_check export -f checksum_create checksum_check
export -f _checksum_check _checksum_create

View file

@ -35,6 +35,9 @@ convert()
# Show status. # Show status.
echo -e "${status}" echo -e "${status}"
# Skip same format.
[[ "${from}" = "${to}" ]] && continue
# Support multiple inputs. # Support multiple inputs.
[[ "${to}" = "mp3" ]] && from="" [[ "${to}" = "mp3" ]] && from=""
@ -51,40 +54,32 @@ convert()
;; ;;
*) *)
echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}" echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}"
((failed++))
false false
;; ;;
esac esac
# Increment failed on error. # Show error.
if [[ $? != 0 ]]; then if [[ ${?} != 0 ]]; then
_convert_error "${target}" echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
# Report result. if [[ ${failed} != 0 ]]; then
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then echo -e "${color_bred}Failed: ${failed}.${color_default}"
if [[ ${failed} = 0 ]]; then false
echo -e "${color_green}All successful.${color_default}"
else
echo -e "${color_bred}Items failed to convert: ${failed}.${color_default}"
fi fi
fi
}
_convert_error()
{
echo -e "${color_bred}${1}: failed.${color_default}"
} }
_convert_gz-xz() _convert_gz-xz()
{ {
pv "${1}" | gzip -d | xz -9e > "${1%.gz}.xz" pv "${1}" | gzip -d | xz -9e > "${1%gz}xz"
} }
_convert_xz-gz() _convert_xz-gz()
{ {
pv "${1}" | xz -d | gzip -1 > "${1%.xz}.gz" pv "${1}" | xz -d | gzip -1 > "${1%xz}gz"
} }
_convert_mp3() _convert_mp3()
@ -93,4 +88,4 @@ _convert_mp3()
} }
# Export. # Export.
export -f convert _convert_gz-xz _convert_xz-gz export -f convert _convert_gz-xz _convert_xz-gz _convert_mp3

View file

@ -6,6 +6,7 @@ name()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed count. local count=0 # processed count.
local total=${#} # total to process. local total=${#} # total to process.
local failed=0
# all targets except hidden by default. # all targets except hidden by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -36,25 +37,30 @@ name()
# status line. # status line.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# check if same name. # check if same name.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && continue
echo -e "${color_green}${status}: No change.${color_default}"
continue
fi
# check if target name already exists. # check if target name already exists.
if [[ -f "${new_name}" ]]; then if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: already exists!${color_default}" echo -e "${color_bred}${status}: already exists!${color_default}"
return 1 ((failed++))
fi fi
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}"
# show change. if [[ ${?} != 0 ]]; then
echo -e "${status}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# rename all files to their hashes while keeping extensions. # rename all files to their hashes while keeping extensions.
@ -65,17 +71,16 @@ name_hash()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local count=0 # processed counter. local count=0 # processed counter.
local total=${#} # total to process. local total=${#} # total to process.
local failed=0
# all targets except hidden by default. # all files except hidden by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only files.
if [[ -f "${target}" ]]; then
# increment count. # increment count.
((count++)) ((count++))
@ -96,23 +101,27 @@ name_hash()
# check if same name. # check if same name.
if [[ "${target}" = "${new_name}" ]]; then if [[ "${target}" = "${new_name}" ]]; then
echo -e "${color_green}${status}: No change.${color_default}" # echo -e "${color_green}${status}: No change.${color_default}"
echo -e "${status}"
continue continue
fi fi
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
# show change. # show change.
echo -e "${status}" echo -e "${status}"
else
# Increment count.
((count++))
# Report skip. # rename target.
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}" mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# check hashes for renamed files. # check hashes for renamed files.
@ -123,51 +132,38 @@ name_hash_check()
local targets=("${@}") # target file(s). local targets=("${@}") # target file(s).
local total=${#} # total to process. local total=${#} # total to process.
local count=0 # processed counter. local count=0 # processed counter.
local failed=0 # failed to process counter. local failed=0
# all targets by default. # all targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only files.
if [[ -f "${target}" ]]; then
# increment count. # increment count.
((count++)) ((count++))
# status info. # status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hashes. # extract hashes.
local stored="${target%%.*}" local stored="${target%%.*}"
local actual=$(sha1sum -- "${target}" | cut -d\ -f1) local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# compare hashes. # compare hashes.
if [[ "${stored}" = "${actual}" ]]; then if [[ "${stored}" != "${actual}" ]]; then
echo -e "${status}: Validation OK." echo -e "${color_bred}${status}: Failed.${color_default}"
else
echo -e "${color_bred}${status}: Validation failed.${color_default}"
((failed++)) ((failed++))
fi fi
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
fi
done done
# report result. if [[ ${failed} != 0 ]]; then
if [[ ${count} -gt 1 ]]; then echo -e "${color_bred}Failed: ${failed}.${color_default}"
if [[ ${failed} -gt 0 ]]; then false
echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}"
fi
fi fi
} }
@ -181,6 +177,7 @@ name_series()
local count=0 # Processed counter. local count=0 # Processed counter.
local episode=0 # Current episode count. local episode=0 # Current episode count.
local total=${#} # Total to process. local total=${#} # Total to process.
local failed=0
# error when no season number specified. # error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
@ -190,14 +187,12 @@ name_series()
# all targets by default. # all targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only targets.
if [[ -f "${target}" ]]; then
# increment episode number. # increment episode number.
((count++)) ((count++))
((episode++)) ((episode++))
@ -207,26 +202,24 @@ name_series()
# prepare status. # prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change. # Warning on no change.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && continue
echo -e "${color_green}${status}: No change.${color_default}"
continue
fi
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}"
# Report status. if [[ ${?} != 0 ]]; then
echo -e "${status}" echo -e "${color_bred}${status}: Failed.${color_default}"
else ((failed++))
# Increment count.
((count++))
# Report skip.
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
fi fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# rename files for Kavita manga format. # rename files for Kavita manga format.
@ -240,6 +233,7 @@ name_manga()
local episode=0 # Current episode count. local episode=0 # Current episode count.
local total=${#} # Total to process. local total=${#} # Total to process.
local manga="${PWD##*/}" # Manga name. local manga="${PWD##*/}" # Manga name.
local failed=0
# error when no season number specified. # error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
@ -249,14 +243,12 @@ name_manga()
# all targets by default. # all targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only targets.
if [[ -f "${target}" ]]; then
# increment episode number. # increment episode number.
((count++)) ((count++))
((episode++)) ((episode++))
@ -266,26 +258,24 @@ name_manga()
# prepare status. # prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change. # Warning on no change.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && continue
echo -e "${color_green}${status}: No change.${color_default}"
continue
fi
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}"
# Show status. if [[ ${?} != 0 ]]; then
echo -e "${status}" echo -e "${color_bred}${status}: Failed.${color_default}"
else ((failed++))
# Increment count.
((count++))
# Report skip.
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
fi fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# rename files for new extension. # rename files for new extension.
@ -297,23 +287,22 @@ name_ext()
local targets=("${@:2}") # target file(s). local targets=("${@:2}") # target file(s).
local count=0 # processed counter. local count=0 # processed counter.
local total=$((${#}-1)) # total to process. local total=$((${#}-1)) # total to process.
local failed=0
# error when no new extension specified. # error when no new extension specified.
if [[ "${extension}" = "" ]]; then if [[ "${extension}" = "" ]]; then
echo "usage: name_ext <EXTENSION> [FILES]" echo "Usage: name_ext <EXTENSION> [FILES]"
return 2 return 2
fi fi
# all targets by default. # all targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=([^.]*) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process. # process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# process only targets.
if [[ -f "${target}" ]]; then
# increment count. # increment count.
((count++)) ((count++))
@ -322,26 +311,24 @@ name_ext()
# Prepare status. # Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change. # Warning on no change.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && continue
echo -e "${color_green}${status}: No change.${color_default}"
continue
fi
# rename target. # rename target.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}"
# show change. if [[ ${?} != 0 ]]; then
echo -e "${status}" echo -e "${color_bred}${status}: Failed.${color_default}"
else ((failed++))
# Increment count.
((count++))
# Report skip.
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
fi fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# Change file's prefixes. # Change file's prefixes.
@ -354,6 +341,7 @@ name_prefix()
local targets=("${@:3}") # Target files. local targets=("${@:3}") # Target files.
local count=0 # Total files processed. local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process. local total=$((${#}-2)) # Total files to process.
local failed=0
# All targets by default. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -371,19 +359,24 @@ name_prefix()
# Prepare status. # Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}" local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change. # Warning on no change.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && continue
echo -e "${color_green}${status}: No change.${color_default}"
continue
fi
# Rename. # Rename.
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}"
# Show change. if [[ ${?} != 0 ]]; then
echo -e "${status}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# export for parallel. # export for parallel.

View file

@ -47,13 +47,16 @@ pack()
;; ;;
"xz") "xz")
_pack_xz "${output}" "${targets[@]}" _pack_xz "${output}" "${targets[@]}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1
;;
esac esac
# actions on error. # Show error.
if [[ $? = 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${color_green}All successful.${color_default}" echo -e "${color_bred}${target}: Failed.${color_default}"
else
echo -e "${color_bred}There were errors.${color_default}"
fi fi
} }
@ -64,8 +67,8 @@ unpack()
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local failed=0
local total=${#targets[@]} local total=${#targets[@]}
local failed=0
# All targets by default. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
@ -120,31 +123,23 @@ unpack()
_unpack_gz "${target}" _unpack_gz "${target}"
;; ;;
*) *)
echo -e "${color_yellow}${target}: format not supported.${color_default}" echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1 ((failed++))
continue
;; ;;
esac esac
# actions on error. # actions on error.
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
_pack_error "${target}" echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
# print report. if [[ ${failed} != 0 ]]; then
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then echo -e "${color_bred}Failed: ${failed}.${color_default}"
if [[ ${failed} = 0 ]]; then false
echo -e "${color_green}All successful.${color_default}"
else
echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}"
fi fi
fi
}
_pack_error()
{
echo -e "${color_bred}${1}: failed.${color_default}"
} }
_pack_zip() _pack_zip()
@ -223,4 +218,4 @@ _unpack_xz()
} }
# export functions. # export functions.
export -f pack unpack _pack_tgz _pack_txz _pack_tar _pack_zip _unpack_zip _unpack_7z _unpack_tgz _unpack_txz _unpack_tar _pack_error _unpack_iso _unpack_rar _pack_gz _pack_xz _unpack_gz _unpack_xz export -f pack unpack _pack_tgz _pack_txz _pack_tar _pack_zip _unpack_zip _unpack_7z _unpack_tgz _unpack_txz _unpack_tar _unpack_iso _unpack_rar _pack_gz _pack_xz _unpack_gz _unpack_xz