Name : utilize _iterate_target.

This commit is contained in:
Dmitry Voronin 2023-12-17 16:27:21 +03:00
parent 49794165f8
commit 72d904dfa2
3 changed files with 165 additions and 359 deletions

View file

@ -10,7 +10,6 @@ function archive() {
[[ "${targets}" = "" ]] && targets=($(_ls_dir)) [[ "${targets}" = "" ]] && targets=($(_ls_dir))
process() { process() {
local target=${1}
local date=$(_archive_date) local date=$(_archive_date)
# Parse name. # Parse name.
@ -20,7 +19,8 @@ function archive() {
local hash=$(tar -c ${target} | pv -s $(/usr/bin/du -sb ${target} | awk '{print $1}') | xz -9e | tee ${name}.txz | sha1sum | cut -d\ -f1) local hash=$(tar -c ${target} | pv -s $(/usr/bin/du -sb ${target} | awk '{print $1}') | xz -9e | tee ${name}.txz | sha1sum | cut -d\ -f1)
# append hash to target name. # append hash to target name.
mv ${name}.txz ${name}_${date}-${hash}.txz local new_name="${name}_${date}-${hash}.txz"
mv -- ${name}.txz ${new_name} && echo ${new_name}
} }
_iterate_targets process ${targets[@]} _iterate_targets process ${targets[@]}
@ -35,7 +35,7 @@ function archive_fast() {
[[ "${targets}" = "" ]] && targets=($(_ls_dir)) [[ "${targets}" = "" ]] && targets=($(_ls_dir))
process() { process() {
local target="${1}" # Start timestamp.
local date=$(_archive_date) local date=$(_archive_date)
# Parse name. # Parse name.
@ -45,7 +45,8 @@ function archive_fast() {
local hash=$(tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | gzip -1 | tee "${name}".tgz | sha1sum | cut -d\ -f1) local hash=$(tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | gzip -1 | tee "${name}".tgz | sha1sum | cut -d\ -f1)
# append hash to target name. # append hash to target name.
mv "${name}".tgz "${name}"_${date}-${hash}.tgz local new_name="${name}_${date}-${hash}.tgz"
mv -- "${name}".tgz ${new_name} && echo ${new_name}
} }
_iterate_targets process ${targets[@]} _iterate_targets process ${targets[@]}
@ -60,8 +61,6 @@ function archive_check() {
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
process() { process() {
local target=${1}
# extract hash from name. # extract hash from name.
local data=($(_archive_parse ${target})) local data=($(_archive_parse ${target}))
local saved=${data[2]} local saved=${data[2]}
@ -96,8 +95,7 @@ function archive_prune() {
local copy_time=${copy_data[1]} local copy_time=${copy_data[1]}
if [[ "${copy_time}" -lt "${time}" ]]; then if [[ "${copy_time}" -lt "${time}" ]]; then
echo -e "${name}: Prune ${copy_time}." rm -- ${copy} && echo "${name}: Prune ${copy_time}."
rm -- ${copy}
fi fi
done done
} }
@ -113,7 +111,7 @@ function archive_rm() {
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern})) [[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
process() { process() {
rm -- "${1}" rm -- "${target}"
} }
_iterate_targets process ${targets[@]} _iterate_targets process ${targets[@]}
@ -123,17 +121,16 @@ function archive_rm() {
# Usage: archive_xz [FILES] # Usage: archive_xz [FILES]
function archive_xz() { function archive_xz() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=(${@})
[[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern_fast}) [[ "${targets}" = "" ]] && targets=$(ls | grep -E ${_archive_pattern_fast})
process() { process() {
local target="${1}"
local data=($(_archive_parse "${target}")) local data=($(_archive_parse "${target}"))
local tmp="${data[0]}.txz" local tmp="${data[0]}.txz"
# Check that old format. # Check that old format.
if [[ "${data[3]}" != "tgz" ]]; then if [[ "${data[3]}" != "tgz" ]]; then
echo -e "${color_bred}Not in .tgz format!${color_default}" _error "Not in .tgz format!"
return 1 return 1
fi fi
@ -141,7 +138,45 @@ function archive_xz() {
local hash=$(pv "${target}" | gzip -d | xz -9e | tee "${tmp}" | sha1sum | cut -d\ -f1) local hash=$(pv "${target}" | gzip -d | xz -9e | tee "${tmp}" | sha1sum | cut -d\ -f1)
# Rename. # Rename.
mv -- "${tmp}" "${data[0]}_${data[1]}-${hash}.txz" && rm "${target}" local new_name="${data[0]}_${data[1]}-${hash}.txz"
mv -- ${tmp} ${new_name} && rm ${target} && echo ${new_name}
}
_iterate_targets process ${targets[@]}
}
# Rename archives.
# If no name specified, it simplifies archive's name.
# If no archives specified, apply to all archives.
# Usage: archive_name [ARCHIVE] [NAME]
function archive_name() {
local IFS=$'\n'
local targets="${1}"
local name="${2}"
[[ "${targets}" = "" ]] && targets=($(ls | grep -E ${_archive_pattern}))
process() {
# simplify name by default.
if [[ "${name}" = "" || ${count} -gt 1 ]]; then
name="${target%_*}"
name="$(parse_camel ${name})"
fi
# remove old name.
local data="${target##*_}"
local new_name="${name}_${data}"
# check for the same name.
[[ "${target}" = "${new_name}" ]] && return 0
# check for existing target.
if [[ -f "${new_name}" ]]; then
_error "${new_name}: Already exists!"
return 1
fi
# rename.
mv -- ${target} ${new_name} && echo ${new_name}
} }
_iterate_targets process ${targets[@]} _iterate_targets process ${targets[@]}
@ -173,7 +208,8 @@ function unarchive() {
;; ;;
esac esac
else else
false _error "Validation failed!"
return 1
fi fi
} }

View file

@ -3,22 +3,10 @@
# Usage: name [FILES] # Usage: name [FILES]
function name() { function name() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=(${@})
local count=0 [[ "${targets}" = "" ]] && targets=([^.]*)
local total=${#}
local failed=0
# all targets except hidden by default.
if [[ "${targets}" = "" ]]; then
targets=([^.]*)
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment counter.
((count++))
process() {
# parse new name. # parse new name.
local ext="" local ext=""
local name="${target}" local name="${target}"
@ -35,32 +23,20 @@ function name() {
# Get new name. # Get new name.
local new_name=$(parse_simplify "${name}")${ext,,} local new_name=$(parse_simplify "${name}")${ext,,}
# status line.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${color_bwhite}${status}${color_default}"
# check if same name. # check if same name.
[[ "${target}" = "${new_name}" ]] && continue [[ "${target}" = "${new_name}" ]] && return 0
# 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}" _error "${new_name}: Already exists!"
((failed++)) return 1
fi fi
# rename target. # rename target.
mv -- "${target}" "${new_name}" mv -- "${target}" "${new_name}"
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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. # Rename all files to their hashes while keeping extensions.
@ -68,22 +44,10 @@ function name() {
# Usage: name_hash [FILES] # Usage: name_hash [FILES]
function name_hash() { function name_hash() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=(${@})
local count=0 [[ "${targets}" = "" ]] && targets=($(_ls_file))
local total=${#}
local failed=0
# all files except hidden by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
process() {
# extract extension. # extract extension.
local extension="${target##*.}" local extension="${target##*.}"
if [[ "${extension}" = "${target}" ]]; then if [[ "${extension}" = "${target}" ]]; then
@ -96,32 +60,14 @@ function name_hash() {
local hash=$(pv "${target}" | sha1sum | cut -d\ -f1) local hash=$(pv "${target}" | sha1sum | cut -d\ -f1)
new_name="${hash,,}${extension,,}" new_name="${hash,,}${extension,,}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# check if same name. # check if same name.
if [[ "${target}" = "${new_name}" ]]; then [[ "${target}" = "${new_name}" ]] && return 0
# echo -e "${color_green}${status}: No change.${color_default}"
echo -e "${color_bwhite}${status}${color_default}"
continue
fi
# show change.
echo -e "${color_bwhite}${status}${color_default}"
# rename target. # rename target.
mv -- "${target}" "${new_name}" mv -- ${target} ${new_name} && echo ${new_name}
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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 previously renamed files. # Check hashes for previously renamed files.
@ -129,42 +75,22 @@ function name_hash() {
# Usage: name_hash_check [FILES] # Usage: name_hash_check [FILES]
function name_hash_check() { function name_hash_check() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=(${@})
local total=${#} [[ "${targets}" = "" ]] && targets=([^.]*)
local count=0
local failed=0
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${color_bwhite}${status}${color_default}"
process() {
# extract hashes. # extract hashes.
local stored="${target%%.*}" local stored="${target%%.*}"
local actual=$(pv "${target}" | sha1sum | 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 "${color_bred}${status}: Failed.${color_default}" _error "Validation failed!"
((failed++)) return 1
fi fi
done }
if [[ ${failed} != 0 ]]; then _iterate_targets process ${targets[@]}
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
} }
# Rename files for Jellyfin series, i.e. `Episode S01E01.mkv` # Rename files for Jellyfin series, i.e. `Episode S01E01.mkv`
@ -172,54 +98,31 @@ function name_hash_check() {
# Usage: name_series <SEASON> [FILES] # Usage: name_series <SEASON> [FILES]
function name_series() { function name_series() {
local IFS=$'\n' local IFS=$'\n'
local season="${1}" local season=${1}
local targets=("${@:2}")
local count=0
local episode=0 local episode=0
local total=${#} local targets=(${@:2})
local failed=0 [[ "${targets}" = "" ]] && targets=($(_ls_file))
# error when no season number specified. # Error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
echo "usage: name_series <SEASON> [FILES]" echo "usage: name_series <SEASON> [FILES]"
return 2 return 2
fi fi
# all targets by default. process() {
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment episode number.
((count++))
((episode++)) ((episode++))
# extract new name. # extract new name.
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}" local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
# prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${color_bwhite}${status}${color_default}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# rename target. # rename target.
mv -- "${target}" "${new_name}" mv -- ${target} ${new_name} && echo ${new_name}
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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. # Rename files for Kavita manga format.
@ -227,55 +130,32 @@ function name_series() {
# Usage: name_manga <SEASON> [FILES] # Usage: name_manga <SEASON> [FILES]
function name_manga() { function name_manga() {
local IFS=$'\n' local IFS=$'\n'
local season="${1}" local manga=${PWD##*/}
local targets=("${@:2}") local season=${1}
local count=0
local episode=0 local episode=0
local total=${#} local targets=(${@:2})
local manga="${PWD##*/}" [[ "${targets}" = "" ]] && targets=($(_ls_file))
local failed=0
# error when no season number specified. # Error when no season number specified.
if [[ "${season}" = "" ]]; then if [[ "${season}" = "" ]]; then
echo "usage: name_manga <SEASON> [FILES]" echo "usage: name_manga <SEASON> [FILES]"
return 2 return 2
fi fi
# all targets by default. process() {
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment episode number.
((count++))
((episode++)) ((episode++))
# extract new name. # Extract new name.
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}" local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
# prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${status}"
# Warning on no change. # Rename target.
[[ "${target}" = "${new_name}" ]] && continue mv -- ${target} ${new_name} && echo ${new_name}
}
# rename target. _iterate_targets process ${targets[@]}
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 with new extension. # Rename files with new extension.
@ -283,52 +163,28 @@ function name_manga() {
# Usage: name_ext <EXTENSION> [FILES] # Usage: name_ext <EXTENSION> [FILES]
function name_ext() { function name_ext() {
local IFS=$'\n' local IFS=$'\n'
local extension="${1}" local extension=${1}
local targets=("${@:2}") local targets=(${@:2})
local count=0 [[ "${targets}" = "" ]] && targets=($(_ls_file))
local total=$((${#}-1))
local failed=0
# error when no new extension specified. # Error when no new extension specified.
if [[ "${extension}" = "" ]]; then if [[ "${extension}" = "" ]]; then
help name_ext help name_ext
return 2 return 2
fi fi
# all targets by default. process() {
if [[ "${targets}" = "" ]]; then # Extract new name.
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
# extract new name.
local new_name="${target%.*}"."${extension}" local new_name="${target%.*}"."${extension}"
# Prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${color_bwhite}${status}${color_default}"
# Warning on no change. # Rename target.
[[ "${target}" = "${new_name}" ]] && continue mv -- ${target} ${new_name} && echo ${new_name}
}
# rename target. _iterate_targets process ${targets[@]}
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 name prefix. # Change file name prefix.
@ -336,47 +192,23 @@ function name_ext() {
# Usage: name_prefix <OLD> <NEW> [FILES] # Usage: name_prefix <OLD> <NEW> [FILES]
function name_prefix() { function name_prefix() {
local IFS=$'\n' local IFS=$'\n'
local old="${1}" local old=${1}
local new="${2}" local new=${2}
local targets=("${@:3}") local targets=(${@:3})
local count=0 [[ "${targets}" = "" ]] && targets=(${old}*)
local total=$((${#}-2))
local failed=0
# All matching targets by default.
if [[ "${targets}" = "" ]]; then
targets=(${old}*)
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
process() {
# Create new name. # Create new name.
local new_name="${new}${target#$old}" local new_name="${new}${target#$old}"
# Prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${color_bwhite}${status}${color_default}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename. # Rename.
mv -- "${target}" "${new_name}" mv -- ${target} ${new_name} && echo ${new_name}
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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 name postfix. # Change file name postfix.
@ -384,47 +216,23 @@ function name_prefix() {
# Usage: name_postfix <OLD> <NEW> [FILES] # Usage: name_postfix <OLD> <NEW> [FILES]
function name_postfix() { function name_postfix() {
local IFS=$'\n' local IFS=$'\n'
local old="${1}" local old=${1}
local new="${2}" local new=${2}
local targets=("${@:3}") local targets=(${@:3})
local count=0 [[ "${targets}" = "" ]] && targets=(*${old})
local total=$((${#}-2))
local failed=0
# All matching targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old})
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
process() {
# Create new name. # Create new name.
local new_name="${target%$old}${new}" local new_name="${target%$old}${new}"
# Prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${color_bwhite}${status}${color_default}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename. # Rename.
mv -- "${target}" "${new_name}" mv -- ${target} ${new_name} && echo ${new_name}
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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
} }
# Replace part of the name. # Replace part of the name.
@ -432,67 +240,35 @@ function name_postfix() {
# Usage: name_replace <OLD> <NEW> [FILES] # Usage: name_replace <OLD> <NEW> [FILES]
function name_replace() { function name_replace() {
local IFS=$'\n' local IFS=$'\n'
local old="${1}" local old=${1}
local new="${2}" local new=${2}
local targets=("${@:3}") local targets=(${@:3})
local count=0 [[ "${targets}" = "" ]] && targets=(*${old}*)
local total=$((${#}-2))
local failed=0
# All matching targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old}*)
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
process() {
# Create new name. # Create new name.
local new_name="${target//$old/$new}" local new_name="${target//$old/$new}"
# Prepare status. # Skip on no change.
local status="[${count}/${total}] ${target} -> ${new_name}" [[ "${target}" = "${new_name}" ]] && return 0
echo -e "${color_bwhite}${status}${color_default}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename. # Rename.
mv -- "${target}" "${new_name}" mv -- ${target} ${new_name} && echo ${new_name}
}
if [[ ${?} != 0 ]]; then _iterate_targets process ${targets[@]}
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
} }
# Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10. # Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10.
# Usage: name_fix_numbering [FILES] # Usage: name_fix_numbering [FILES]
function name_fix_numbering() { function name_fix_numbering() {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local highest=0
local count=0
local total=${#}
local failed=0
local power=0 local power=0
local targets=(${@})
# All files by default. [[ "${targets}" = "" ]] && targets=($(ls | grep "^[0-9]"))
if [[ "${targets[*]}" = "" ]]; then
targets=($(ls | grep "^[0-9]"))
total=${#targets[@]}
fi
# Count leading zeroes. # Count leading zeroes.
local highest=0
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# Check that starts with a digit. # Check that starts with a digit.
[[ "${target}" =~ ^[0-9] ]] || continue [[ "${target}" =~ ^[0-9] ]] || continue
@ -510,16 +286,11 @@ function name_fix_numbering() {
i=$((${i}/10)) i=$((${i}/10))
done done
# Process. process() {
for target in "${targets[@]}"; do
((count++))
local status="[${count}/${total}] ${target}"
# Check that starts with a digit. # Check that starts with a digit.
if [[ ! "${target}" =~ ^[0-9] ]]; then if [[ ! "${target}" =~ ^[0-9] ]]; then
echo -e "${color_bwhite}${status}${color_default}" _error "Does not start with a digit!"
continue return 1
fi fi
# Prepare new name. # Prepare new name.
@ -528,32 +299,17 @@ function name_fix_numbering() {
digit=$((10#${digit})) digit=$((10#${digit}))
local new_name=$(printf "%0${power}d" "${digit}")"${target#${digits[0]}}" local new_name=$(printf "%0${power}d" "${digit}")"${target#${digits[0]}}"
status="${status} -> ${new_name}" # Skip if the same name.
[[ "${target}" = "${new_name}" ]] && return 0
# Skip if the same.
if [[ "${target}" = "${new_name}" ]]; then
echo -e "${color_bwhite}${status}${color_default}"
continue
fi
# Check that file does not exist. # Check that file does not exist.
if [[ -e "${new_name}" ]]; then if [[ -e "${new_name}" ]]; then
echo -e "${color_bred}${status}: File exists!${color_default}" _error "${new_name}: File exists!"
return 1 return 1
fi fi
echo -e "${color_bwhite}${status}${color_default}" mv -- ${target} ${new_name} && echo ${new_name}
}
mv -- "${target}" "${new_name}" _iterate_targets process ${targets[@]}
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
} }

View file

@ -10,6 +10,7 @@ function _parse_ints() {
# Function-wrapper to iterate with specified function with provided files. # Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories. # By default Iterates on all non-hidden files and directories.
# List of variables available to FUNCTION: target - current file, count - current item index, total - sum of targets, failed - count of previously failed items, status - status line (not recommended to use).
# Usage: _iterate_targets <FUNCTION> [FILES] # Usage: _iterate_targets <FUNCTION> [FILES]
function _iterate_targets() { function _iterate_targets() {
local IFS=$'\n' local IFS=$'\n'
@ -18,11 +19,12 @@ function _iterate_targets() {
local total=${#targets[@]} local total=${#targets[@]}
local count=0 local count=0
local failed=0 local failed=0
local code=0
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls)) _error "No targets provided."
total=${#targets[@]} return 1
fi fi
# iterate each target. # iterate each target.
@ -42,11 +44,23 @@ function _iterate_targets() {
((failed++)) ((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
fi fi
# Add newline if not the last one.
[[ "${count}" = "${total}" ]] || echo
done done
# Show error. # Show error.
if [[ ${failed} != 0 ]]; then if [[ ${failed} != 0 ]]; then
echo
echo -e "${color_bred}Failed: ${failed}.${color_default}" echo -e "${color_bred}Failed: ${failed}.${color_default}"
false false
fi fi
} }
# Report error to stdout.
# Always returns code 1.
# If no message specified, prints "An error has occured".
# Usage: _error [MESSAGE]
function _error() {
echo -e "${color_bred}${*}${color_default}"
}