bash : reduce output.
This commit is contained in:
parent
a3d12ece74
commit
aa50193fb7
|
@ -359,9 +359,8 @@ Command|Description
|
|||
|
||||
Command|Description
|
||||
---|---
|
||||
`checksum_create`|Create checksums for files in current directory.
|
||||
`checksum_check`|Check previously created checksums.
|
||||
`checksum_update`|Check old hashes and create new ones for all the files.
|
||||
`checksum_create [FILES]`|Create checksums for files in current directory.
|
||||
`checksum_check [FILES]`|Check previously created checksums.
|
||||
|
||||
## Chmod.
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@ archive()
|
|||
local IFS=$'\n'
|
||||
local targets=("${@}") # target file(s).
|
||||
local count=0 # processed count.
|
||||
local failed=0
|
||||
local total=${#} # total to process.
|
||||
local date=$(_ARCHIVE_DATE) # date stamp.
|
||||
local failed=0
|
||||
|
||||
# Set dafult value to target all directories.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -31,18 +31,23 @@ archive()
|
|||
echo -e "${status}"
|
||||
|
||||
# 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.
|
||||
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.
|
||||
if [[ ${failed} = 0 ]]; then
|
||||
echo -e "${color_green}${status}: Done.${color_default}"
|
||||
else
|
||||
# Show error.
|
||||
if [[ ${?} != 0 ]]; then
|
||||
((failed++))
|
||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
||||
fi
|
||||
done
|
||||
|
||||
# Show error.
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
# archive file with minimal compression and checksum.
|
||||
|
@ -52,9 +57,9 @@ archive_fast()
|
|||
local IFS=$'\n'
|
||||
local targets=("${@}") # target file(s).
|
||||
local count=0 # processed count.
|
||||
local failed=0
|
||||
local total=${#} # total to process.
|
||||
local date=$(_ARCHIVE_DATE) # date stamp.
|
||||
local failed=0
|
||||
|
||||
# Set dafult value to target all directories.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -72,18 +77,23 @@ archive_fast()
|
|||
echo -e "${status}"
|
||||
|
||||
# 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.
|
||||
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.
|
||||
if [[ ${failed} = 0 ]]; then
|
||||
echo -e "${color_green}${status}: Done.${color_default}"
|
||||
else
|
||||
# Show error.
|
||||
if [[ $? != 0 ]]; then
|
||||
((failed++))
|
||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
||||
fi
|
||||
done
|
||||
|
||||
# Show error.
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
# check archive hashes.
|
||||
|
@ -94,7 +104,7 @@ archive_check()
|
|||
local targets=("${@}") # target file(s).
|
||||
local total=${#} # total to process.
|
||||
local count=0 # processed count.
|
||||
local failed=0 # total failed checks.
|
||||
local failed=0
|
||||
|
||||
# set dafult value to target all supported archives.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -104,14 +114,12 @@ archive_check()
|
|||
|
||||
# iterate each target.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only files.
|
||||
[[ -f "${target}" ]] || continue
|
||||
|
||||
# increment counter.
|
||||
((count++))
|
||||
|
||||
# status info.
|
||||
local status="[${count}/${total}] ${target}"
|
||||
echo -e "${status}"
|
||||
|
||||
# extract hash from name.
|
||||
local saved="${target##*-}"
|
||||
|
@ -121,19 +129,15 @@ archive_check()
|
|||
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
||||
|
||||
# compare hashes, show error on mismatch.
|
||||
if [[ "${actual}" = "${saved}" ]]; then
|
||||
echo -e "${status}: Validation OK."
|
||||
else
|
||||
echo -e "${color_bred}${status}: Validation failed.${color_default}"
|
||||
if [[ "${actual}" != "${saved}" ]]; then
|
||||
((failed++))
|
||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
||||
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 [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -144,8 +148,8 @@ archive_prune()
|
|||
local IFS=$'\n'
|
||||
local targets=("${@}")
|
||||
local count=0
|
||||
local failed=0
|
||||
local total=${#}
|
||||
local failed=0
|
||||
|
||||
# All archives by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -173,16 +177,19 @@ archive_prune()
|
|||
|
||||
if [[ "${copy_time}" -lt "${time}" ]]; then
|
||||
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
|
||||
done
|
||||
done
|
||||
|
||||
# Report result.
|
||||
if [[ ${failed} = 0 ]]; then
|
||||
echo -e "${color_green}All successful.${color_default}"
|
||||
else
|
||||
echo -e "${color_bred}Items failed to prune: ${failed}.${color_default}"
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -194,6 +201,7 @@ unarchive()
|
|||
local targets=("${@}") # target file(s).
|
||||
local count=0 # processed count.
|
||||
local total=${#} # total to process.
|
||||
local failed=0
|
||||
|
||||
# set dafult value to target all supported archives.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -208,6 +216,7 @@ unarchive()
|
|||
|
||||
# status info.
|
||||
local status="[${count}/${total}] ${target}"
|
||||
echo -e "${status}"
|
||||
|
||||
# extract hash from name.
|
||||
local saved="${target##*-}"
|
||||
|
@ -218,8 +227,6 @@ unarchive()
|
|||
|
||||
# extract if hash matched or show error if not.
|
||||
if [[ "${saved}" = "${actual}" ]]; then
|
||||
echo -e "${status}: Validation OK."
|
||||
|
||||
# figure out the compression tool.
|
||||
local compressor
|
||||
case "${target##*.}" in
|
||||
|
@ -234,15 +241,23 @@ unarchive()
|
|||
# extract.
|
||||
unset IFS
|
||||
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.
|
||||
echo -e "${color_green}${status}: Done.${color_default}"
|
||||
if [[ ${?} != 0 ]]; then
|
||||
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
|
||||
|
||||
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.
|
||||
|
@ -254,6 +269,7 @@ archive_name()
|
|||
local name="${2}" # new name.
|
||||
local total=1 # total targets to process.
|
||||
local count=0 # processed targets counter.
|
||||
local failed=0
|
||||
|
||||
# set dafult value to target all supported archives.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -263,9 +279,6 @@ archive_name()
|
|||
|
||||
# iterate each target.
|
||||
for target in "${targets[@]}"; do
|
||||
# only work with files.
|
||||
[[ -f "${target}" ]] || continue
|
||||
|
||||
# iterate counter.
|
||||
((count++))
|
||||
|
||||
|
@ -284,19 +297,32 @@ archive_name()
|
|||
|
||||
# check for the same name.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
echo -e "${status}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# check for existing target.
|
||||
if [[ -f "${new_name}" ]]; then
|
||||
echo -e "${color_bred}${status}: Already exists.${color_default}"
|
||||
return 1
|
||||
((failed++))
|
||||
continue
|
||||
fi
|
||||
|
||||
echo -e "${status}"
|
||||
|
||||
# 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
|
||||
|
||||
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.
|
||||
|
|
|
@ -1,53 +1,111 @@
|
|||
# Save file checksums.
|
||||
# Usage: checksum_create [FILES]
|
||||
checksum_create()
|
||||
{
|
||||
local file=".checksum"
|
||||
local IFS=$'\n'
|
||||
local targets=("${@}")
|
||||
local count=0
|
||||
local total=${#}
|
||||
local failed=0
|
||||
|
||||
# Error if checksum already exists.
|
||||
if [[ -f "${file}" ]]; then
|
||||
echo -e "${color_bred}Checksum already exists.${color_default}"
|
||||
return 1
|
||||
# All files by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
find -type f | parallel -j $(_core_count) -- sha1sum {} >> "${file}"
|
||||
sed -i "/${file}/d" ${file}
|
||||
# Iterate each 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.
|
||||
checksum_check()
|
||||
{
|
||||
local file=".checksum"
|
||||
local IFS=$'\n'
|
||||
local targets=("${@}")
|
||||
local count=0
|
||||
local total=${#}
|
||||
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
|
||||
echo -e "${color_green}All successful.${color_default}"
|
||||
return 0
|
||||
else
|
||||
# echo -e "${color_bred}Items failed to validate: ${failed}.${color_default}"
|
||||
return 1
|
||||
# Iterate each 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 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
|
||||
}
|
||||
|
||||
# Check old values and create new one if all was fine.
|
||||
checksum_update()
|
||||
_checksum_create()
|
||||
{
|
||||
local file=".checksum"
|
||||
|
||||
if [[ ! -f "${file}" ]]; then
|
||||
echo -e "${color_bred}Checksum doesn't exist.${color_default}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
checksum_check && rm "${file}" && checksum_create
|
||||
local path="${1%/*}"
|
||||
local name="${1##*/}"
|
||||
sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
|
||||
}
|
||||
|
||||
_checksum_check()
|
||||
{
|
||||
local file="${1##*\ \ }"
|
||||
local stored="${1%%\ \ *}"
|
||||
local failed=0
|
||||
|
||||
# Skip if no file.
|
||||
[[ -f "${file}" ]] || return 0
|
||||
|
@ -65,4 +123,5 @@ _checksum_check()
|
|||
return 0
|
||||
}
|
||||
|
||||
export -f _checksum_check
|
||||
export -f checksum_create checksum_check
|
||||
export -f _checksum_check _checksum_create
|
||||
|
|
|
@ -35,6 +35,9 @@ convert()
|
|||
# Show status.
|
||||
echo -e "${status}"
|
||||
|
||||
# Skip same format.
|
||||
[[ "${from}" = "${to}" ]] && continue
|
||||
|
||||
# Support multiple inputs.
|
||||
[[ "${to}" = "mp3" ]] && from=""
|
||||
|
||||
|
@ -51,40 +54,32 @@ convert()
|
|||
;;
|
||||
*)
|
||||
echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}"
|
||||
((failed++))
|
||||
false
|
||||
;;
|
||||
esac
|
||||
|
||||
# Increment failed on error.
|
||||
if [[ $? != 0 ]]; then
|
||||
_convert_error "${target}"
|
||||
# Show error.
|
||||
if [[ ${?} != 0 ]]; then
|
||||
echo -e "${color_bred}${target}: Failed.${color_default}"
|
||||
((failed++))
|
||||
fi
|
||||
done
|
||||
|
||||
# Report result.
|
||||
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then
|
||||
if [[ ${failed} = 0 ]]; then
|
||||
echo -e "${color_green}All successful.${color_default}"
|
||||
else
|
||||
echo -e "${color_bred}Items failed to convert: ${failed}.${color_default}"
|
||||
fi
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
_convert_error()
|
||||
{
|
||||
echo -e "${color_bred}${1}: failed.${color_default}"
|
||||
}
|
||||
|
||||
_convert_gz-xz()
|
||||
{
|
||||
pv "${1}" | gzip -d | xz -9e > "${1%.gz}.xz"
|
||||
pv "${1}" | gzip -d | xz -9e > "${1%gz}xz"
|
||||
}
|
||||
|
||||
_convert_xz-gz()
|
||||
{
|
||||
pv "${1}" | xz -d | gzip -1 > "${1%.xz}.gz"
|
||||
pv "${1}" | xz -d | gzip -1 > "${1%xz}gz"
|
||||
}
|
||||
|
||||
_convert_mp3()
|
||||
|
@ -93,4 +88,4 @@ _convert_mp3()
|
|||
}
|
||||
|
||||
# Export.
|
||||
export -f convert _convert_gz-xz _convert_xz-gz
|
||||
export -f convert _convert_gz-xz _convert_xz-gz _convert_mp3
|
||||
|
|
|
@ -6,6 +6,7 @@ name()
|
|||
local targets=("${@}") # target file(s).
|
||||
local count=0 # processed count.
|
||||
local total=${#} # total to process.
|
||||
local failed=0
|
||||
|
||||
# all targets except hidden by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -36,25 +37,30 @@ name()
|
|||
|
||||
# status line.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
echo -e "${status}"
|
||||
|
||||
# check if same name.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
[[ "${target}" = "${new_name}" ]] && continue
|
||||
|
||||
# check if target name already exists.
|
||||
if [[ -f "${new_name}" ]]; then
|
||||
echo -e "${color_bred}${status}: already exists!${color_default}"
|
||||
return 1
|
||||
((failed++))
|
||||
fi
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
# show change.
|
||||
echo -e "${status}"
|
||||
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
|
||||
}
|
||||
|
||||
# rename all files to their hashes while keeping extensions.
|
||||
|
@ -65,54 +71,57 @@ name_hash()
|
|||
local targets=("${@}") # target file(s).
|
||||
local count=0 # processed counter.
|
||||
local total=${#} # total to process.
|
||||
local failed=0
|
||||
|
||||
# all targets except hidden by default.
|
||||
# all files except hidden by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=([^.]*)
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
# process.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only files.
|
||||
if [[ -f "${target}" ]]; then
|
||||
# increment count.
|
||||
((count++))
|
||||
# increment count.
|
||||
((count++))
|
||||
|
||||
# extract extension.
|
||||
local extension="${target##*.}"
|
||||
if [[ "${extension}" = "${target}" ]]; then
|
||||
extension=""
|
||||
else
|
||||
extension=".${extension}"
|
||||
fi
|
||||
|
||||
# hash the new name.
|
||||
local hash=$(sha1sum -- "${target}" | cut -d\ -f1)
|
||||
new_name="${hash,,}${extension,,}"
|
||||
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
|
||||
# check if same name.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
|
||||
# show change.
|
||||
echo -e "${status}"
|
||||
# extract extension.
|
||||
local extension="${target##*.}"
|
||||
if [[ "${extension}" = "${target}" ]]; then
|
||||
extension=""
|
||||
else
|
||||
# Increment count.
|
||||
((count++))
|
||||
extension=".${extension}"
|
||||
fi
|
||||
|
||||
# Report skip.
|
||||
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
||||
# hash the new name.
|
||||
local hash=$(sha1sum -- "${target}" | cut -d\ -f1)
|
||||
new_name="${hash,,}${extension,,}"
|
||||
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
|
||||
# check if same name.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
# echo -e "${color_green}${status}: No change.${color_default}"
|
||||
echo -e "${status}"
|
||||
continue
|
||||
fi
|
||||
|
||||
# show change.
|
||||
echo -e "${status}"
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
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 hashes for renamed files.
|
||||
|
@ -123,51 +132,38 @@ name_hash_check()
|
|||
local targets=("${@}") # target file(s).
|
||||
local total=${#} # total to process.
|
||||
local count=0 # processed counter.
|
||||
local failed=0 # failed to process counter.
|
||||
local failed=0
|
||||
|
||||
# all targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=([^.]*)
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
# process.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only files.
|
||||
if [[ -f "${target}" ]]; then
|
||||
# increment count.
|
||||
((count++))
|
||||
# increment count.
|
||||
((count++))
|
||||
|
||||
# status info.
|
||||
local status="[${count}/${total}] ${target}"
|
||||
# status info.
|
||||
local status="[${count}/${total}] ${target}"
|
||||
|
||||
# extract hashes.
|
||||
local stored="${target%%.*}"
|
||||
local actual=$(sha1sum -- "${target}" | cut -d\ -f1)
|
||||
echo -e "${status}"
|
||||
|
||||
# compare hashes.
|
||||
if [[ "${stored}" = "${actual}" ]]; then
|
||||
echo -e "${status}: Validation OK."
|
||||
else
|
||||
echo -e "${color_bred}${status}: Validation failed.${color_default}"
|
||||
((failed++))
|
||||
fi
|
||||
else
|
||||
# Increment count.
|
||||
((count++))
|
||||
# extract hashes.
|
||||
local stored="${target%%.*}"
|
||||
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
|
||||
|
||||
# Report skip.
|
||||
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
||||
# compare hashes.
|
||||
if [[ "${stored}" != "${actual}" ]]; then
|
||||
echo -e "${color_bred}${status}: Failed.${color_default}"
|
||||
((failed++))
|
||||
fi
|
||||
done
|
||||
|
||||
# report result.
|
||||
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
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -181,6 +177,7 @@ name_series()
|
|||
local count=0 # Processed counter.
|
||||
local episode=0 # Current episode count.
|
||||
local total=${#} # Total to process.
|
||||
local failed=0
|
||||
|
||||
# error when no season number specified.
|
||||
if [[ "${season}" = "" ]]; then
|
||||
|
@ -190,43 +187,39 @@ name_series()
|
|||
|
||||
# all targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=([^.]*)
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
# process.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only targets.
|
||||
if [[ -f "${target}" ]]; then
|
||||
# increment episode number.
|
||||
((count++))
|
||||
((episode++))
|
||||
# increment episode number.
|
||||
((count++))
|
||||
((episode++))
|
||||
|
||||
# extract new name.
|
||||
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
|
||||
# extract new name.
|
||||
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
|
||||
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
echo -e "${status}"
|
||||
|
||||
# Warning on no change.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
# Warning on no change.
|
||||
[[ "${target}" = "${new_name}" ]] && continue
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
|
||||
# Report status.
|
||||
echo -e "${status}"
|
||||
else
|
||||
# Increment count.
|
||||
((count++))
|
||||
|
||||
# Report skip.
|
||||
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# rename files for Kavita manga format.
|
||||
|
@ -240,6 +233,7 @@ name_manga()
|
|||
local episode=0 # Current episode count.
|
||||
local total=${#} # Total to process.
|
||||
local manga="${PWD##*/}" # Manga name.
|
||||
local failed=0
|
||||
|
||||
# error when no season number specified.
|
||||
if [[ "${season}" = "" ]]; then
|
||||
|
@ -249,43 +243,39 @@ name_manga()
|
|||
|
||||
# all targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=([^.]*)
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
# process.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only targets.
|
||||
if [[ -f "${target}" ]]; then
|
||||
# increment episode number.
|
||||
((count++))
|
||||
((episode++))
|
||||
# increment episode number.
|
||||
((count++))
|
||||
((episode++))
|
||||
|
||||
# extract new name.
|
||||
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
|
||||
# extract new name.
|
||||
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
|
||||
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
# prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
echo -e "${status}"
|
||||
|
||||
# Warning on no change.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
# Warning on no change.
|
||||
[[ "${target}" = "${new_name}" ]] && continue
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
|
||||
# Show status.
|
||||
echo -e "${status}"
|
||||
else
|
||||
# Increment count.
|
||||
((count++))
|
||||
|
||||
# Report skip.
|
||||
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# rename files for new extension.
|
||||
|
@ -297,51 +287,48 @@ name_ext()
|
|||
local targets=("${@:2}") # target file(s).
|
||||
local count=0 # processed counter.
|
||||
local total=$((${#}-1)) # total to process.
|
||||
local failed=0
|
||||
|
||||
# error when no new extension specified.
|
||||
if [[ "${extension}" = "" ]]; then
|
||||
echo "usage: name_ext <EXTENSION> [FILES]"
|
||||
echo "Usage: name_ext <EXTENSION> [FILES]"
|
||||
return 2
|
||||
fi
|
||||
|
||||
# all targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
targets=([^.]*)
|
||||
targets=($(ls --classify | grep -v /\$))
|
||||
total=${#targets[@]}
|
||||
fi
|
||||
|
||||
# process.
|
||||
for target in "${targets[@]}"; do
|
||||
# process only targets.
|
||||
if [[ -f "${target}" ]]; then
|
||||
# increment count.
|
||||
((count++))
|
||||
# increment count.
|
||||
((count++))
|
||||
|
||||
# extract new name.
|
||||
local new_name="${target%.*}"."${extension}"
|
||||
# extract new name.
|
||||
local new_name="${target%.*}"."${extension}"
|
||||
|
||||
# Prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
# Prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
echo -e "${status}"
|
||||
|
||||
# Warning on no change.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
# Warning on no change.
|
||||
[[ "${target}" = "${new_name}" ]] && continue
|
||||
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
|
||||
# show change.
|
||||
echo -e "${status}"
|
||||
else
|
||||
# Increment count.
|
||||
((count++))
|
||||
|
||||
# Report skip.
|
||||
echo -e "${color_green}[${count}/${total}] ${target}: Skipping directory.${color_default}"
|
||||
# rename target.
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Change file's prefixes.
|
||||
|
@ -354,6 +341,7 @@ name_prefix()
|
|||
local targets=("${@:3}") # Target files.
|
||||
local count=0 # Total files processed.
|
||||
local total=$((${#}-2)) # Total files to process.
|
||||
local failed=0
|
||||
|
||||
# All targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -371,19 +359,24 @@ name_prefix()
|
|||
|
||||
# Prepare status.
|
||||
local status="[${count}/${total}] ${target} -> ${new_name}"
|
||||
echo -e "${status}"
|
||||
|
||||
# Warning on no change.
|
||||
if [[ "${target}" = "${new_name}" ]]; then
|
||||
echo -e "${color_green}${status}: No change.${color_default}"
|
||||
continue
|
||||
fi
|
||||
[[ "${target}" = "${new_name}" ]] && continue
|
||||
|
||||
# Rename.
|
||||
mv -- "${target}" "${new_name}" &> /dev/null
|
||||
|
||||
# Show change.
|
||||
echo -e "${status}"
|
||||
mv -- "${target}" "${new_name}"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# export for parallel.
|
||||
|
|
|
@ -47,13 +47,16 @@ pack()
|
|||
;;
|
||||
"xz")
|
||||
_pack_xz "${output}" "${targets[@]}"
|
||||
;;
|
||||
*)
|
||||
echo -e "${color_bred}${target}: Format not supported.${color_default}"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# actions on error.
|
||||
if [[ $? = 0 ]]; then
|
||||
echo -e "${color_green}All successful.${color_default}"
|
||||
else
|
||||
echo -e "${color_bred}There were errors.${color_default}"
|
||||
# Show error.
|
||||
if [[ $? != 0 ]]; then
|
||||
echo -e "${color_bred}${target}: Failed.${color_default}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -64,8 +67,8 @@ unpack()
|
|||
local IFS=$'\n'
|
||||
local targets=("${@}")
|
||||
local count=0
|
||||
local failed=0
|
||||
local total=${#targets[@]}
|
||||
local failed=0
|
||||
|
||||
# All targets by default.
|
||||
if [[ "${targets}" = "" ]]; then
|
||||
|
@ -120,33 +123,25 @@ unpack()
|
|||
_unpack_gz "${target}"
|
||||
;;
|
||||
*)
|
||||
echo -e "${color_yellow}${target}: format not supported.${color_default}"
|
||||
return 1
|
||||
echo -e "${color_bred}${target}: Format not supported.${color_default}"
|
||||
((failed++))
|
||||
continue
|
||||
;;
|
||||
esac
|
||||
|
||||
# actions on error.
|
||||
if [[ $? != 0 ]]; then
|
||||
_pack_error "${target}"
|
||||
echo -e "${color_bred}${target}: Failed.${color_default}"
|
||||
((failed++))
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# print report.
|
||||
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then
|
||||
if [[ ${failed} = 0 ]]; then
|
||||
echo -e "${color_green}All successful.${color_default}"
|
||||
else
|
||||
echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}"
|
||||
fi
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
_pack_error()
|
||||
{
|
||||
echo -e "${color_bred}${1}: failed.${color_default}"
|
||||
}
|
||||
|
||||
_pack_zip()
|
||||
{
|
||||
zip -9 -r "${@}"
|
||||
|
@ -223,4 +218,4 @@ _unpack_xz()
|
|||
}
|
||||
|
||||
# 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
|
||||
|
|
Reference in a new issue