bash : reindent with tabs.

This commit is contained in:
Dmitry Voronin 2023-12-05 21:50:45 +03:00
parent f62d9e41a5
commit a095e63b15
30 changed files with 1513 additions and 1513 deletions

View file

@ -1,391 +1,391 @@
_ARCHIVE_PATTERN="_[0-9]{12}-[[:alnum:]]{40}.t[xg]z"
_ARCHIVE_DATE()
{
date +%Y%m%d%H%M
date +%Y%m%d%H%M
}
# archive file with maximum compression and checksum.
# usage: archive [FILES]
archive()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
# Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep /\$))
total=${#targets[@]}
fi
# Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep /\$))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
local name=$(parse_camel "${target}")
local name=$(parse_camel "${target}")
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${name}".txz
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${name}".txz
# append hash to target name.
mv "${name}".txz "${name}"_${date}-$(pv "${name}".txz | sha1sum | cut -d\ -f1).txz
# append hash to target name.
mv "${name}".txz "${name}"_${date}-$(pv "${name}".txz | sha1sum | cut -d\ -f1).txz
# Show error.
if [[ ${?} != 0 ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
# 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
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# archive file with minimal compression and checksum.
# usage: archive_fast [FILES]
archive_fast()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local date=$(_ARCHIVE_DATE) # date stamp.
local failed=0
# Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep /$))
total=${#targets[@]}
fi
# Set dafult value to target all directories.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep /$))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
local name=$(parse_camel "${target}")
local name=$(parse_camel "${target}")
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${name}".tgz
# create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${name}".tgz
# append hash to target name.
mv "${name}".tgz "${name}"_${date}-$(pv "${name}".tgz | sha1sum | cut -d\ -f1).tgz
# append hash to target name.
mv "${name}".tgz "${name}"_${date}-$(pv "${name}".tgz | sha1sum | cut -d\ -f1).tgz
# Show error.
if [[ $? != 0 ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
# 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
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# check archive hashes.
# usage: archive_check [FILES]
archive_check()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local total=${#} # total to process.
local count=0 # processed count.
local failed=0
local IFS=$'\n'
local targets=("${@}") # target file(s).
local total=${#} # total to process.
local count=0 # processed count.
local failed=0
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hash from name.
local data=($(_archive_parse ${target}))
local saved=${data[2]}
# extract hash from name.
local data=($(_archive_parse ${target}))
local saved=${data[2]}
# calculate actual hash.
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# calculate actual hash.
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# compare hashes, show error on mismatch.
if [[ "${actual}" != "${saved}" ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
# compare hashes, show error on mismatch.
if [[ "${actual}" != "${saved}" ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# Delete old versions of an archives.
# Usage: archive_prune [NAME]
archive_prune()
{
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
# All archives by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# All archives by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# Iterate each target.
for target in "${targets[@]}"; do
# Only work with existing files.
[[ -f "${target}" ]] || continue
# Iterate each target.
for target in "${targets[@]}"; do
# Only work with existing files.
[[ -f "${target}" ]] || continue
# Iterate counter.
((count++))
# Iterate counter.
((count++))
local data=($(_archive_parse ${target}))
local name="${data[0]}"
local time="${data[1]}"
local copies=($(ls ${name}_*))
local data=($(_archive_parse ${target}))
local name="${data[0]}"
local time="${data[1]}"
local copies=($(ls ${name}_*))
# Iterate each copy.
for copy in "${copies[@]}"; do
local copy_data=($(_archive_parse ${copy}))
local copy_time="${copy_data[1]}"
# Iterate each copy.
for copy in "${copies[@]}"; do
local copy_data=($(_archive_parse ${copy}))
local copy_time="${copy_data[1]}"
if [[ "${copy_time}" -lt "${time}" ]]; then
echo -e "${name}: prune ${copy_time}."
rm -- "${copy}"
if [[ "${copy_time}" -lt "${time}" ]]; then
echo -e "${name}: prune ${copy_time}."
rm -- "${copy}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
fi
done
done
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
fi
done
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# extract previously created archive with checksum validation.
# usage: unarchive [FILES]
unarchive()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local failed=0
local IFS=$'\n'
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
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# extract hash from name.
local data=($(_archive_parse "${target}"))
local saved="${data[2]}"
# extract hash from name.
local data=($(_archive_parse "${target}"))
local saved="${data[2]}"
# calculate actual hash.
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# calculate actual hash.
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# extract if hash matched or show error if not.
if [[ "${saved}" = "${actual}" ]]; then
# figure out the compression tool.
local compressor
case "${target##*.}" in
"txz")
compressor="xz -d"
;;
"tgz")
compressor="gzip -d"
;;
esac
# extract if hash matched or show error if not.
if [[ "${saved}" = "${actual}" ]]; then
# figure out the compression tool.
local compressor
case "${target##*.}" in
"txz")
compressor="xz -d"
;;
"tgz")
compressor="gzip -d"
;;
esac
# extract.
unset IFS
pv "${target}" | ${compressor} | tar -xf -
# extract.
unset IFS
pv "${target}" | ${compressor} | tar -xf -
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 [[ ${?} != 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
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.
# usage: archive_name [ARCHIVE] [NAME]
archive_name()
{
local IFS=$'\n'
local targets="${1}" # target archive(s).
local name="${2}" # new name.
local total=1 # total targets to process.
local count=0 # processed targets counter.
local failed=0
local IFS=$'\n'
local targets="${1}" # target archive(s).
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
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_ARCHIVE_PATTERN}))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# iterate counter.
((count++))
# iterate each target.
for target in "${targets[@]}"; do
# iterate counter.
((count++))
# simplify name by default.
if [[ "${name}" = "" || ${count} -gt 1 ]]; then
name="${target%_*}"
name="$(parse_camel ${name})"
fi
# 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}"
# remove old name.
local data="${target##*_}"
local new_name="${name}_${data}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# check for the same name.
if [[ "${target}" = "${new_name}" ]]; then
echo -e "${status}"
continue
fi
# check for the same name.
if [[ "${target}" = "${new_name}" ]]; then
echo -e "${status}"
continue
fi
# check for existing target.
if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: Already exists.${color_default}"
((failed++))
continue
fi
# check for existing target.
if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: Already exists.${color_default}"
((failed++))
continue
fi
echo -e "${status}"
echo -e "${status}"
# rename.
mv -- "${target}" "${new_name}"
# rename.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
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.
archive_convert()
{
local IFS=$'\n'
local old_format="_[[:alnum:]]{40}.tar.[xg]z"
local targets=($(ls | grep -E ${old_format}))
local IFS=$'\n'
local old_format="_[[:alnum:]]{40}.tar.[xg]z"
local targets=($(ls | grep -E ${old_format}))
# add timestamp.
for target in "${targets[@]}"; do
local stamp=$(stat --format '%w' -- "${target}" | sed -e 's/\..*//' -e 's/:..$//' -e 's/-//g' -e 's/://' -e 's/\ //')
local name="${target%_*}"
local old_data="${target##*_}"
local new_name="${name}_${stamp}-${old_data}"
# add timestamp.
for target in "${targets[@]}"; do
local stamp=$(stat --format '%w' -- "${target}" | sed -e 's/\..*//' -e 's/:..$//' -e 's/-//g' -e 's/://' -e 's/\ //')
local name="${target%_*}"
local old_data="${target##*_}"
local new_name="${name}_${stamp}-${old_data}"
echo "${target} -> ${new_name}"
echo "${target} -> ${new_name}"
mv "${target}" "${new_name}"
done
mv "${target}" "${new_name}"
done
# convert tar.xz and tar.gz to .tgz and .txz.
old_format="_[0-9]{12}-[[:alnum:]]{40}.tar.[xg]z"
targets=($(ls | grep -E ${old_format}))
# convert tar.xz and tar.gz to .tgz and .txz.
old_format="_[0-9]{12}-[[:alnum:]]{40}.tar.[xg]z"
targets=($(ls | grep -E ${old_format}))
for target in "${targets[@]}"; do
local compression="${target##*.}"
local new_compression
for target in "${targets[@]}"; do
local compression="${target##*.}"
local new_compression
case "${compression}" in
"gz")
new_compression="tgz"
;;
"xz")
new_compression="txz"
;;
esac
case "${compression}" in
"gz")
new_compression="tgz"
;;
"xz")
new_compression="txz"
;;
esac
local new_name="${target%.tar.*}".${new_compression}
local new_name="${target%.tar.*}".${new_compression}
echo "${target} -> ${new_name}"
echo "${target} -> ${new_name}"
mv -- "${target}" "${new_name}"
done
mv -- "${target}" "${new_name}"
done
}
_archive_parse()
{
local input="${1}"
local name="${input%_*}"
local format="${input##*.}"
local data="${input##*_}"; data="${data%.*}"
local date="${data%%-*}"
local hash="${data##*-}"
local input="${1}"
local name="${input%_*}"
local format="${input##*.}"
local data="${input##*_}"; data="${data%.*}"
local date="${data%%-*}"
local hash="${data##*-}"
echo "${name}"
echo "${date}"
echo "${hash}"
echo "${format}"
echo "${name}"
echo "${date}"
echo "${hash}"
echo "${format}"
}
# export everything, primarily for use with parallel..
@ -395,26 +395,26 @@ export _ARCHIVE_PATTERN
# autocomplete.
_archive_name()
{
local IFS=$'\n'
COMPREPLY=()
local IFS=$'\n'
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "$(ls | grep -E ${_ARCHIVE_PATTERN})" -- ${cur}) )
return 0
else
local name="${prev%_*}"
COMPREPLY=( $(compgen -W "${name}" -- ${cur}) )
return 0
fi
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "$(ls | grep -E ${_ARCHIVE_PATTERN})" -- ${cur}) )
return 0
else
local name="${prev%_*}"
COMPREPLY=( $(compgen -W "${name}" -- ${cur}) )
return 0
fi
}
_archive_grep()
{
_autocomplete_grep ${_ARCHIVE_PATTERN}
_autocomplete_grep ${_ARCHIVE_PATTERN}
}
complete -o filenames -F _archive_grep archive_check unarchive

View file

@ -3,65 +3,65 @@
# there are also options like -o nospace. see man for more info.
_autocomplete()
{
local IFS=$'\n'
local commands="${*}"
local IFS=$'\n'
local commands="${*}"
COMPREPLY=()
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
}
# autocomplete only first argument.
_autocomplete_first()
{
local IFS=$'\n'
local commands="${*}"
local IFS=$'\n'
local commands="${*}"
COMPREPLY=()
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
fi
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
fi
}
# Autocomplete by grepping file names.
_autocomplete_grep()
{
local IFS=$'\n'
COMPREPLY=()
local IFS=$'\n'
COMPREPLY=()
local pattern="${1}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
local pattern="${1}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
COMPREPLY=( $(compgen -W "$(ls | grep -E ${pattern})" -- ${cur}) )
return 0
COMPREPLY=( $(compgen -W "$(ls | grep -E ${pattern})" -- ${cur}) )
return 0
}
# autocomplete nested program.
_autocomplete_nested()
{
# local IFS=$'\n'
local cur prev words cword split i
_init_completion -s || return
# local IFS=$'\n'
local cur prev words cword split i
_init_completion -s || return
for ((i = 1; i <= cword; i++)); do
if [[ ${words[i]} != -* ]]; then
local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
local root_command=${words[i]}
_command_offset ${i}
return
fi
done
for ((i = 1; i <= cword; i++)); do
if [[ ${words[i]} != -* ]]; then
local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
local root_command=${words[i]}
_command_offset ${i}
return
fi
done
}

View file

@ -1,31 +1,31 @@
# install Cargo/Rust.
bootstrap_rust()
{
curl https://sh.rustup.rs -sSf | sh
rustup component add rust-analyzer
curl https://sh.rustup.rs -sSf | sh
rustup component add rust-analyzer
}
# install TeXLive.
bootstrap_texlive()
{
cd /tmp
wget https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
tar -xf install-tl-unx.tar.gz
rm install-tl-unx.tar.gz
cd ./install-tl-*
./install-tl
cd /tmp
wget https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
tar -xf install-tl-unx.tar.gz
rm install-tl-unx.tar.gz
cd ./install-tl-*
./install-tl
}
# install grub theme.
bootstrap_grub()
{
wget -O- https://github.com/shvchk/fallout-grub-theme/raw/master/install.sh | bash
echo 'GRUB_HIDDEN_TIMEOUT=' >> /etc/default/grub
grub2-mkconfig -o /etc/grub2.cfg
wget -O- https://github.com/shvchk/fallout-grub-theme/raw/master/install.sh | bash
echo 'GRUB_HIDDEN_TIMEOUT=' >> /etc/default/grub
grub2-mkconfig -o /etc/grub2.cfg
}
# install ffmpeg.
bootstrap_ffmpeg()
{
flatpak install org.kde.kdenlive
flatpak install org.kde.kdenlive
}

View file

@ -2,36 +2,36 @@
# Usage: cdd <DIR>
cdd()
{
local target="${1}"
local array
local result
IFS='/' read -r -a array <<< "${PWD}"
array=("${array[@]:1}")
local target="${1}"
local array
local result
IFS='/' read -r -a array <<< "${PWD}"
array=("${array[@]:1}")
# Make search case-insensitive.
shopt -s nocasematch
# Make search case-insensitive.
shopt -s nocasematch
# Find desired dir.
# for dir in "${array[@]}"; do
# result="${result}/${dir}"
# [[ "${dir}" =~ "${target}" ]] && break
# done
# Find desired dir.
# for dir in "${array[@]}"; do
# result="${result}/${dir}"
# [[ "${dir}" =~ "${target}" ]] && break
# done
local found=1
for (( idx=${#array[@]}-2 ; idx>=0 ; idx-- )); do
dir="${array[idx]}"
[[ "${dir}" =~ "${target}" ]] && found=0
[[ ${found} = 0 ]] && result="/${dir}${result}"
done
local found=1
for (( idx=${#array[@]}-2 ; idx>=0 ; idx-- )); do
dir="${array[idx]}"
[[ "${dir}" =~ "${target}" ]] && found=0
[[ ${found} = 0 ]] && result="/${dir}${result}"
done
# Clean-up???
shopt -u nocasematch
# Clean-up???
shopt -u nocasematch
# Go there!
if [[ "${result}" != "" ]]; then
echo "${result}"
cd "${result}"
else
return 1
fi
# Go there!
if [[ "${result}" != "" ]]; then
echo "${result}"
cd "${result}"
else
return 1
fi
}

View file

@ -2,132 +2,132 @@
# Usage: checksum_create [FILES]
checksum_create()
{
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Increment count.
((count++))
# Increment count.
((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash exists.
[[ -f "${hashfile}" ]] && continue
# Skip if hash exists.
[[ -f "${hashfile}" ]] && continue
# Calculate hash.
pv "${target}" | sha1sum > ${hashfile}
# Calculate hash.
pv "${target}" | sha1sum > ${hashfile}
# Report success.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
# 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}"
return 1
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
return 1
fi
[[ ${total} = 0 ]] && return 1
[[ ${total} = 0 ]] && return 1
}
# Check stored values against actual files.
checksum_check()
{
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
local skipped=0
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#}
local failed=0
local skipped=0
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Iterate each file.
for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1"
# Increment count.
((count++))
# Increment count.
((count++))
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
# Skip if hash doesn't exist.
[[ -f "${hashfile}" ]] || { ((skipped++)); continue; }
# 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)
# 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 [[ "${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}"
return 1
fi
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
return 1
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
return 1
fi
}
checksum()
{
find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1
find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1
}
_checksum_create()
{
local path="${1%/*}"
local name="${1##*/}"
sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
local path="${1%/*}"
local name="${1##*/}"
sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
}
_checksum_check()
{
local file="${1##*\ \ }"
local stored="${1%%\ \ *}"
local file="${1##*\ \ }"
local stored="${1%%\ \ *}"
# Skip if no file.
[[ -f "${file}" ]] || return 0
# Skip if no file.
[[ -f "${file}" ]] || return 0
# Check file hash.
local actual=$(sha1sum "${file}")
actual="${actual%%\ \ *}"
# Check file hash.
local actual=$(sha1sum "${file}")
actual="${actual%%\ \ *}"
# Compare values.
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${file}: Failed.${color_default}"
return 1
fi
# Compare values.
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${file}: Failed.${color_default}"
return 1
fi
return 0
return 0
}
export -f checksum_create checksum_check

View file

@ -21,19 +21,19 @@ export color_byellow="\033[1;33m"
# print all available colors with their names colored in corresponding color.
color_test()
{
echo -e " ${color_default}color_default\n\
${color_blue}color_blue\n\
${color_bblue}color_bblue\n\
${color_cyan}color_cyan\n\
${color_bcyan}color_bcyan\n\
${color_green}color_green\n\
${color_bgreen}color_bgreen\n\
${color_purple}color_purple\n\
${color_bpurple}color_bpurple\n\
${color_red}color_red\n\
${color_bred}color_bred\n\
${color_white}color_white\n\
${color_bwhite}color_bwhite\n\
${color_yellow}color_yellow\n\
${color_byellow}color_byellow"
echo -e " ${color_default}color_default\n\
${color_blue}color_blue\n\
${color_bblue}color_bblue\n\
${color_cyan}color_cyan\n\
${color_bcyan}color_bcyan\n\
${color_green}color_green\n\
${color_bgreen}color_bgreen\n\
${color_purple}color_purple\n\
${color_bpurple}color_bpurple\n\
${color_red}color_red\n\
${color_bred}color_bred\n\
${color_white}color_white\n\
${color_bwhite}color_bwhite\n\
${color_yellow}color_yellow\n\
${color_byellow}color_byellow"
}

View file

@ -1,2 +1,2 @@
alias copy="wl-copy" # copy stdin to system clipboard.
alias copy="wl-copy" # copy stdin to system clipboard.
alias paste="wl-paste" # paste system clipboard to stdout.

View file

@ -1,4 +1,4 @@
_GDCONF_PATH="${HOME}/.config/linux/gnome.dconf"
alias dconf_load="sed -i -e s/voronind/$(whoami)/g ${_GDCONF_PATH} ; dconf load / < ${_GDCONF_PATH}" # load gnome settings.
alias dconf_load="sed -i -e s/voronind/$(whoami)/g ${_GDCONF_PATH} ; dconf load / < ${_GDCONF_PATH}" # load gnome settings.
alias dconf_save="dconf dump / > gnome.dconf" # dump gnome settings to a file.

View file

@ -21,47 +21,47 @@ alias dcs="docker compose stop"
# usage: dcdu [SERVICES]
dcdu()
{
dcd "${@}"
dcu "${@}"
dcd "${@}"
dcu "${@}"
}
# pull & up specified services.
# usage: dcpu [SERVICES]
dcpu()
{
dcp "${@}"
dcu "${@}"
dcp "${@}"
dcu "${@}"
}
# up & attach to logs for specified services.
# usage: dcul [SERVICES]
dcul()
{
dcu "${@}" && dcl "${@}"
dcu "${@}" && dcl "${@}"
}
# find out container's IP address.
# usage: docker_up <CONTAINER NAME>
docker_ip()
{
docker inspect -f '\''{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'\' $1 | sed "s/^.//" | sed "s/.$//"
docker inspect -f '\''{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'\' $1 | sed "s/^.//" | sed "s/.$//"
}
# update all docker images.
docker_update()
{
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull
}
# autocomplete.
_dc_services()
{
_autocomplete "$(docker compose config --services 2> /dev/null)"
_autocomplete "$(docker compose config --services 2> /dev/null)"
}
_dc_containers()
{
_autocomplete "$(docker ps --format "\""{{.Names}}"\"")"
_autocomplete "$(docker ps --format "\""{{.Names}}"\"")"
}
complete -F _dc_services dcu dcd dcp dcl dcul dcdu dcr dcs dcpu

View file

@ -2,95 +2,95 @@
# usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>
ffmpeg_mux_audio()
{
if [[ "$1" = "" ]]; then
echo "usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>"
return 2
fi
if [[ "$1" = "" ]]; then
echo "usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>"
return 2
fi
for file in *; do ffmpeg -i "$file" -i "$1"/"$file" -c copy -map 0:v:0 -map 1:a:0 -shortest "$2"/"$file"; done
for file in *; do ffmpeg -i "$file" -i "$1"/"$file" -c copy -map 0:v:0 -map 1:a:0 -shortest "$2"/"$file"; done
}
# Mux cover into music file.
# Usage: ffmpeg_mux_cover <FORMAT> <COVER>
ffmpeg_mux_cover()
{
if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_mux_cover <FORMAT> <COVER>"
return 2
fi
if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_mux_cover <FORMAT> <COVER>"
return 2
fi
local format="${1}"
local cover="${2}"
local format="${1}"
local cover="${2}"
mkdir out
mkdir out
case "${format}" in
# "mka"|"mkv")
# for file in *.${format}; do
# ffmpeg -i "${file}" -attach "${cover}" -map 0 -c copy -metadata:s:t mimetype="image/${cover##*.}" -metadata:s:t:0 filename="cover.${cover##*.}" "./out/${file}" || return 1
# done
# ;;
*)
for file in *.${format}; do
ffmpeg -i "${file}" -i "${cover}" -map 0 -map 0:-v? -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic ./out/"${file}" || return 1
done
;;
esac
case "${format}" in
# "mka"|"mkv")
# for file in *.${format}; do
# ffmpeg -i "${file}" -attach "${cover}" -map 0 -c copy -metadata:s:t mimetype="image/${cover##*.}" -metadata:s:t:0 filename="cover.${cover##*.}" "./out/${file}" || return 1
# done
# ;;
*)
for file in *.${format}; do
ffmpeg -i "${file}" -i "${cover}" -map 0 -map 0:-v? -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic ./out/"${file}" || return 1
done
;;
esac
mv out/* .
rm -d out/ && rm "${2}"
mv out/* .
rm -d out/ && rm "${2}"
}
# Change music metadata.
# Usage: ffmpeg_music_meta <FORMAT>
ffmpeg_music_meta()
{
if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_music_meta <FORMAT>"
return 2
fi
if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_music_meta <FORMAT>"
return 2
fi
local format="${1}"
local format="${1}"
ls *.${format} &> /dev/null || return 1
ls *.${format} &> /dev/null || return 1
local artist="${PWD%/*}"; artist="${artist##*/}"; artist="${artist//_/ }"
local album="${PWD##*/}"; album="${album#*_}"; album="${album//_/ }"
local year="${PWD##*/}"; year="${year%%_*}"
# local total=$(ls *.${format} | wc -l)
local artist="${PWD%/*}"; artist="${artist##*/}"; artist="${artist//_/ }"
local album="${PWD##*/}"; album="${album#*_}"; album="${album//_/ }"
local year="${PWD##*/}"; year="${year%%_*}"
# local total=$(ls *.${format} | wc -l)
mkdir out
mkdir out
for file in *.${format}; do
local track="${file%%_*}"; track=$((10#${track})); [[ "${track}" = "" ]] && track=0
local title="${file#*_}"; title="${title%.*}"; title="${title//_/ }"
for file in *.${format}; do
local track="${file%%_*}"; track=$((10#${track})); [[ "${track}" = "" ]] && track=0
local title="${file#*_}"; title="${title%.*}"; title="${title//_/ }"
# echo "${artist}; ${album}; ${year}; ${track}; ${title}"
# TODO: make it format-specific.
ffmpeg -i "${file}" -map 0 -c copy -metadata "artist=${artist}" -metadata "album_artist=${artist}" -metadata "album=${album}" -metadata "date=${year}" -metadata "year=${year}" -metadata "date_released=${year}" -metadata "track=${track}" -metadata "part_number=${track}" -metadata "title=${title}" ./out/"${file}" || return 1
done
# echo "${artist}; ${album}; ${year}; ${track}; ${title}"
# TODO: make it format-specific.
ffmpeg -i "${file}" -map 0 -c copy -metadata "artist=${artist}" -metadata "album_artist=${artist}" -metadata "album=${album}" -metadata "date=${year}" -metadata "year=${year}" -metadata "date_released=${year}" -metadata "track=${track}" -metadata "part_number=${track}" -metadata "title=${title}" ./out/"${file}" || return 1
done
mv out/* .
rm -d out/
mv out/* .
rm -d out/
}
_ffprobe_fps()
{
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}")
fps="${fps%%/*}"
echo "${fps}"
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}")
fps="${fps%%/*}"
echo "${fps}"
}
_ffprobe_keyint()
{
local fps=$(_ffprobe_fps "${1}")
echo $((fps*5))
local fps=$(_ffprobe_fps "${1}")
echo $((fps*5))
}
_ffprobe_ba()
{
local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}")
[[ "${ba}" != "N/A" ]] && echo $((ba/1024)) || echo 128
local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}")
[[ "${ba}" != "N/A" ]] && echo $((ba/1024)) || echo 128
}
export -f _ffprobe_keyint _ffprobe_ba _ffprobe_fps

View file

@ -1,6 +1,6 @@
# Find all file extensions.
find_ext()
{
local types=($(find -type f | sed -e "s/.*\///" -e "s/^\.//" -e "/\./!d" -e "s/.*\.//"))
echo "${types[@]}" | tr ' ' '\n' | sort -u
local types=($(find -type f | sed -e "s/.*\///" -e "s/^\.//" -e "/\./!d" -e "s/.*\.//"))
echo "${types[@]}" | tr ' ' '\n' | sort -u
}

View file

@ -3,15 +3,15 @@
# SPEED is one of 10/100/1000 etc.
fix_ethernet_speed()
{
local device="${1}"
local speed="${2}"
local device="${1}"
local speed="${2}"
if [[ "${device}" = "" || "${speed}" = "" ]]; then
echo "usage: fix_ethernet_speed <DEVICE> <SPEED>"
return 2
fi
if [[ "${device}" = "" || "${speed}" = "" ]]; then
echo "usage: fix_ethernet_speed <DEVICE> <SPEED>"
return 2
fi
ethtool -s "${device}" speed "${speed}"
ethtool -s "${device}" speed "${speed}"
}
# fix files wrong sftp password.

View file

@ -22,125 +22,125 @@ alias gp="git apply"
# usage: ga [FILES]
ga()
{
local target=${@}
local target=${@}
if [[ "${target}" = "" ]]; then
target="."
fi
if [[ "${target}" = "" ]]; then
target="."
fi
git diff ${target}
git add ${target}
git diff ${target}
git add ${target}
}
# rebase by X commits or from root. when COUNT is 0 - rebase from root. default is 2.
# usage: gr [COMMIT COUNT]
gr()
{
local base="${1}"
local base="${1}"
# rebase last 2 commits by default.
if [[ "${base}" = "" ]]; then
base="2"
fi
# rebase last 2 commits by default.
if [[ "${base}" = "" ]]; then
base="2"
fi
# if 0, rebase from root. else from specified base.
if [[ "${base}" = "0" ]]; then
git rebase -i --root
else
git rebase -i HEAD~${base}
fi
# if 0, rebase from root. else from specified base.
if [[ "${base}" = "0" ]]; then
git rebase -i --root
else
git rebase -i HEAD~${base}
fi
}
# specify git user as Dmitry Voronin with provided email.
# usage: gu [EMAIL]
gu()
{
local name="Dmitry Voronin"
local email="${1}"
local name="Dmitry Voronin"
local email="${1}"
if [[ "${name}" = "" || "${email}" = "" ]]; then
echo "usage: gu [EMAIL]"
return 2
fi
if [[ "${name}" = "" || "${email}" = "" ]]; then
echo "usage: gu [EMAIL]"
return 2
fi
git config user.name "${name}"
git config user.email "${email}"
git config user.name "${name}"
git config user.email "${email}"
}
# Get my git repo.
# Usage: gg <REPO>
gg()
{
git clone https://git.voronind.com/voronind/"${1}"
git clone https://git.voronind.com/voronind/"${1}"
}
# See diff for a specific commit.
# Usage: gdc <COMMITHASH>
gdc()
{
git diff "${1}^!"
git diff "${1}^!"
}
# Show current branch.
_git_current_branch()
{
git branch --show-current 2> /dev/null
git branch --show-current 2> /dev/null
}
# Show origin's url.
_git_origin_url()
{
git remote get-url origin
git remote get-url origin
}
# Get this dotfiles url.
_git_dotfiles_url()
{
echo 'https://git.voronind.com/voronind/linux.git'
echo 'https://git.voronind.com/voronind/linux.git'
}
# Check if current git repo is this dotfiles.
_git_is_dotfiles()
{
# [[ "$(_git_origin_url)" = "$(_git_dotfiles_url)" ]]
local dir="${PWD}"
# [[ "$(_git_origin_url)" = "$(_git_dotfiles_url)" ]]
local dir="${PWD}"
while [[ "${dir}" != "" ]]; do
if [[ -d "${dir}/.git" ]]; then
while [[ "${dir}" != "" ]]; do
if [[ -d "${dir}/.git" ]]; then
if [[ "${dir}" = "${HOME}" ]] || [[ "${dir}" = "$(realpath ${HOME})" ]]; then
return 0
else
return 1
fi
fi
return 0
else
return 1
fi
fi
dir="${dir%/*}"
done
dir="${dir%/*}"
done
}
# autocomplete.
_completion_loader git &> /dev/null
__git_complete gps _git_push &> /dev/null
__git_complete gpsf _git_push &> /dev/null
__git_complete gpl _git_pull &> /dev/null
__git_complete gl _git_log &> /dev/null
__git_complete gs _git_status &> /dev/null
__git_complete gst _git_stash &> /dev/null
__git_complete gd _git_diff &> /dev/null
__git_complete gdc _git_diff &> /dev/null
__git_complete gc _git_commit &> /dev/null
__git_complete gch _git_checkout &> /dev/null
_completion_loader git &> /dev/null
__git_complete gps _git_push &> /dev/null
__git_complete gpsf _git_push &> /dev/null
__git_complete gpl _git_pull &> /dev/null
__git_complete gl _git_log &> /dev/null
__git_complete gs _git_status &> /dev/null
__git_complete gst _git_stash &> /dev/null
__git_complete gd _git_diff &> /dev/null
__git_complete gdc _git_diff &> /dev/null
__git_complete gc _git_commit &> /dev/null
__git_complete gch _git_checkout &> /dev/null
__git_complete gchb _git_checkout &> /dev/null
__git_complete gb _git_branch &> /dev/null
__git_complete gbd _git_branch &> /dev/null
__git_complete gf _git_fetch &> /dev/null
__git_complete gt _git_tag &> /dev/null
__git_complete gp _git_apply &> /dev/null
__git_complete ga _git_add &> /dev/null
__git_complete gb _git_branch &> /dev/null
__git_complete gbd _git_branch &> /dev/null
__git_complete gf _git_fetch &> /dev/null
__git_complete gt _git_tag &> /dev/null
__git_complete gp _git_apply &> /dev/null
__git_complete ga _git_add &> /dev/null
_gu()
{
_autocomplete_first account@voronind.com dd.voronin@fsight.ru
_autocomplete_first account@voronind.com dd.voronin@fsight.ru
}
complete -F _gu gu

View file

@ -5,46 +5,46 @@ unset l ll lll llll la lla &> /dev/null
# list files in dir.
l()
{
ls -lhv --si --group-directories-first "$@"
ls -lhv --si --group-directories-first "$@"
}
# list last modified files first.
ll()
{
ls -lhvtr --si "$@"
ls -lhvtr --si "$@"
}
# list files in tree structure.
# usage: lll [DEPTH] [DIRS]
lll()
{
local IFS=$'\n'
local depth="${1}"
local target=("${@:2}")
local IFS=$'\n'
local depth="${1}"
local target=("${@:2}")
[[ "${target}" = "" ]] && target="."
[[ "${depth}" = "" ]] && depth=666
[[ "${depth}" = "-" ]] && depth=666
[[ "${target}" = "" ]] && target="."
[[ "${depth}" = "" ]] && depth=666
[[ "${depth}" = "-" ]] && depth=666
tree -a -L "${depth}" -- "${target[@]}"
tree -a -L "${depth}" -- "${target[@]}"
}
# list files recursively.
llll()
{
ls -RlAhv --si --group-directories-first "$@"
ls -RlAhv --si --group-directories-first "$@"
}
# list all files in dir, incl. hidden files.
la()
{
ls -lAh --si --group-directories-first "$@"
ls -lAh --si --group-directories-first "$@"
}
# list all files in dir, incl. hidden files, sorted by mtime.
lla()
{
ls -lAhtr --si "$@"
ls -lAhtr --si "$@"
}
# export.

View file

@ -2,476 +2,476 @@
# usage: name [FILES]
name()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed count.
local total=${#} # total to process.
local failed=0
local IFS=$'\n'
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
targets=([^.]*)
total=${#targets[@]}
fi
# all targets except hidden by default.
if [[ "${targets}" = "" ]]; then
targets=([^.]*)
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# process.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# parse new name.
local ext=""
local name="${target}"
# parse new name.
local ext=""
local name="${target}"
# ext only for files.
if [[ -f "${target}" ]]; then
ext=".${target##*.}"
name="${target%.*}"
fi
# ext only for files.
if [[ -f "${target}" ]]; then
ext=".${target##*.}"
name="${target%.*}"
fi
# Files w/o extension support.
[[ "${ext#.}" = "${name}" ]] && ext=""
# Files w/o extension support.
[[ "${ext#.}" = "${name}" ]] && ext=""
# Get new name.
local new_name=$(parse_simplify "${name}")${ext,,}
# Get new name.
local new_name=$(parse_simplify "${name}")${ext,,}
# status line.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# status line.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# check if same name.
[[ "${target}" = "${new_name}" ]] && continue
# check if same name.
[[ "${target}" = "${new_name}" ]] && continue
# check if target name already exists.
if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: already exists!${color_default}"
((failed++))
fi
# check if target name already exists.
if [[ -f "${new_name}" ]]; then
echo -e "${color_bred}${status}: already exists!${color_default}"
((failed++))
fi
# rename target.
mv -- "${target}" "${new_name}"
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# rename all files to their hashes while keeping extensions.
# usage: name_hash [FILES]
name_hash()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed counter.
local total=${#} # total to process.
local failed=0
local IFS=$'\n'
local targets=("${@}") # target file(s).
local count=0 # processed counter.
local total=${#} # total to process.
local failed=0
# all files except hidden by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# 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.
for target in "${targets[@]}"; do
# increment count.
((count++))
# extract extension.
local extension="${target##*.}"
if [[ "${extension}" = "${target}" ]]; then
extension=""
else
extension=".${extension}"
fi
# extract extension.
local extension="${target##*.}"
if [[ "${extension}" = "${target}" ]]; then
extension=""
else
extension=".${extension}"
fi
# hash the new name.
local hash=$(pv "${target}" | sha1sum | cut -d\ -f1)
new_name="${hash,,}${extension,,}"
# hash the new name.
local hash=$(pv "${target}" | sha1sum | cut -d\ -f1)
new_name="${hash,,}${extension,,}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# 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
# 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}"
# show change.
echo -e "${status}"
# rename target.
mv -- "${target}" "${new_name}"
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# check hashes for renamed files.
# usage: name_hash_check [FILES]
name_hash_check()
{
local IFS=$'\n'
local targets=("${@}") # target file(s).
local total=${#} # total to process.
local count=0 # processed counter.
local failed=0
local IFS=$'\n'
local targets=("${@}") # target file(s).
local total=${#} # total to process.
local count=0 # processed counter.
local failed=0
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
# status info.
local status="[${count}/${total}] ${target}"
# status info.
local status="[${count}/${total}] ${target}"
echo -e "${status}"
echo -e "${status}"
# extract hashes.
local stored="${target%%.*}"
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# extract hashes.
local stored="${target%%.*}"
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
# compare hashes.
if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
# compare hashes.
if [[ "${stored}" != "${actual}" ]]; 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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# rename files for Jellyfin series, i.e. Episode S01E01.mkv
# usage: name_series <SEASON> [FILES]
name_series()
{
local IFS=$'\n'
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
local count=0 # Processed counter.
local episode=0 # Current episode count.
local total=${#} # Total to process.
local failed=0
local IFS=$'\n'
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
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
echo "usage: name_series <SEASON> [FILES]"
return 2
fi
# error when no season number specified.
if [[ "${season}" = "" ]]; then
echo "usage: name_series <SEASON> [FILES]"
return 2
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment episode number.
((count++))
((episode++))
# process.
for target in "${targets[@]}"; do
# 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}"
echo -e "${status}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# rename target.
mv -- "${target}" "${new_name}"
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# rename files for Kavita manga format.
# usage: name_manga <SEASON> [FILES]
name_manga()
{
local IFS=$'\n'
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
local count=0 # Processed counter.
local episode=0 # Current episode count.
local total=${#} # Total to process.
local manga="${PWD##*/}" # Manga name.
local failed=0
local IFS=$'\n'
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
local count=0 # Processed counter.
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
echo "usage: name_manga <SEASON> [FILES]"
return 2
fi
# error when no season number specified.
if [[ "${season}" = "" ]]; then
echo "usage: name_manga <SEASON> [FILES]"
return 2
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment episode number.
((count++))
((episode++))
# process.
for target in "${targets[@]}"; do
# 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}"
echo -e "${status}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# rename target.
mv -- "${target}" "${new_name}"
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# rename files for new extension.
# usage: name_ext <EXTENSION> [FILES]
name_ext()
{
local IFS=$'\n'
local extension="${1}" # new extension.
local targets=("${@:2}") # target file(s).
local count=0 # processed counter.
local total=$((${#}-1)) # total to process.
local failed=0
local IFS=$'\n'
local extension="${1}" # new extension.
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]"
return 2
fi
# error when no new extension specified.
if [[ "${extension}" = "" ]]; then
echo "Usage: name_ext <EXTENSION> [FILES]"
return 2
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$))
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# increment count.
((count++))
# process.
for target in "${targets[@]}"; do
# 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}"
echo -e "${status}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# rename target.
mv -- "${target}" "${new_name}"
# rename target.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# Change file prefixes.
# Usage: name_prefix <OLD> <NEW> [FILES]
name_prefix()
{
local IFS=$'\n'
local old="${1}" # Old prefix.
local new="${2}" # New prefix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
local failed=0
local IFS=$'\n'
local old="${1}" # Old prefix.
local new="${2}" # New 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
targets=(${old}*)
total=${#targets[@]}
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(${old}*)
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Create new name.
local new_name="${new}${target#$old}"
# Create new name.
local new_name="${new}${target#$old}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename.
mv -- "${target}" "${new_name}"
# Rename.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# Change file postfix.
# Usage: name_postfix <OLD> <NEW> [FILES]
name_postfix()
{
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
local failed=0
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
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
targets=(*${old})
total=${#targets[@]}
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old})
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Create new name.
local new_name="${target%$old}${new}"
# Create new name.
local new_name="${target%$old}${new}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename.
mv -- "${target}" "${new_name}"
# Rename.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
# Replace part of the name.
name_replace()
{
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
local failed=0
local IFS=$'\n'
local old="${1}" # Old postfix.
local new="${2}" # New postfix.
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
targets=(*${old}*)
total=${#targets[@]}
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=(*${old}*)
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Create new name.
local new_name="${target//$old/$new}"
# Create new name.
local new_name="${target//$old/$new}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
echo -e "${status}"
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Warning on no change.
[[ "${target}" = "${new_name}" ]] && continue
# Rename.
mv -- "${target}" "${new_name}"
# Rename.
mv -- "${target}" "${new_name}"
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
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
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}

View file

@ -1,11 +1,11 @@
# send Telegram notification.
notify()
{
curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null
curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null
}
# send silent Telegram notification.
notify_silent()
{
curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\",\"disable_notification\":\"true\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null
curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\",\"disable_notification\":\"true\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null
}

View file

@ -2,22 +2,22 @@
# usage: own [USER] [FILES]
own()
{
local file="${2}"
local user="${1}"
local file="${2}"
local user="${1}"
# default to current dir.
if [ "${file}" = "" ]; then
file='.'
fi
# default to current dir.
if [ "${file}" = "" ]; then
file='.'
fi
# default to current user.
if [ "${user}" = "" ]; then
user="${UID}"
fi
# default to current user.
if [ "${user}" = "" ]; then
user="${UID}"
fi
# set ownership.
chown "${user}":"${user}" -R "${file}" &> /dev/null
# set ownership.
chown "${user}":"${user}" -R "${file}" &> /dev/null
# remove access from group and others.
chmod -077 -R "${file}"
# remove access from group and others.
chmod -077 -R "${file}"
}

View file

@ -4,217 +4,217 @@ _UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$"
# Usage: pack <TARGET.ext> [FILES]
pack()
{
local IFS=$'\n'
local output="${1}"
local targets=("${@:2}")
local format="${output##*.}"
local name="${output%.*}"
local IFS=$'\n'
local output="${1}"
local targets=("${@:2}")
local format="${output##*.}"
local name="${output%.*}"
# report no output.
if [[ "${output}" = "" ]]; then
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no output.
if [[ "${output}" = "" ]]; then
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no format.
if [[ "${format}" = "" ]]; then
echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no format.
if [[ "${format}" = "" ]]; then
echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls))
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls))
fi
# process.
case "${format}" in
"tgz")
_pack_tgz "${output}" "${targets[@]}"
;;
"txz")
_pack_txz "${output}" "${targets[@]}"
;;
"tar")
_pack_tar "${output}" "${targets[@]}"
;;
"zip")
_pack_zip "${output}" "${targets[@]}"
;;
"gz")
_pack_gz "${output}" "${targets[@]}"
;;
"xz")
_pack_xz "${output}" "${targets[@]}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1
;;
esac
# process.
case "${format}" in
"tgz")
_pack_tgz "${output}" "${targets[@]}"
;;
"txz")
_pack_txz "${output}" "${targets[@]}"
;;
"tar")
_pack_tar "${output}" "${targets[@]}"
;;
"zip")
_pack_zip "${output}" "${targets[@]}"
;;
"gz")
_pack_gz "${output}" "${targets[@]}"
;;
"xz")
_pack_xz "${output}" "${targets[@]}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1
;;
esac
# Show error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
fi
# Show error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
fi
}
# attempt to unpack everything.
# usage: unpack [FILES]
unpack()
{
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#targets[@]}
local failed=0
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#targets[@]}
local failed=0
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
total=${#targets[@]}
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
total=${#targets[@]}
fi
# process all files.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# process all files.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# prepare status.
local status="[${count}/${total}] ${target}"
# prepare status.
local status="[${count}/${total}] ${target}"
# show status.
echo -e "${status}"
# show status.
echo -e "${status}"
# unpack file type.
local type="${target##*.}"
# unpack file type.
local type="${target##*.}"
[[ "${target}" =~ .tar.gz$ ]] && type="tar.gz"
[[ "${target}" =~ .tar.xz$ ]] && type="tar.xz"
[[ "${target}" =~ .tar.gz$ ]] && type="tar.gz"
[[ "${target}" =~ .tar.xz$ ]] && type="tar.xz"
# unpack content.
case "${type,,}" in
"zip")
_unpack_zip "${target}"
;;
"7z")
_unpack_7z "${target}"
;;
"tgz"|"tar.gz")
_unpack_tgz "${target}"
;;
"txz"|"tar.xz")
_unpack_txz "${target}"
;;
"tar")
_unpack_tar "${target}"
;;
"iso")
_unpack_iso "${target}"
;;
"rar")
_unpack_rar "${target}"
;;
"xz")
_unpack_xz "${target}"
;;
"gz")
_unpack_gz "${target}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
((failed++))
continue
;;
esac
# unpack content.
case "${type,,}" in
"zip")
_unpack_zip "${target}"
;;
"7z")
_unpack_7z "${target}"
;;
"tgz"|"tar.gz")
_unpack_tgz "${target}"
;;
"txz"|"tar.xz")
_unpack_txz "${target}"
;;
"tar")
_unpack_tar "${target}"
;;
"iso")
_unpack_iso "${target}"
;;
"rar")
_unpack_rar "${target}"
;;
"xz")
_unpack_xz "${target}"
;;
"gz")
_unpack_gz "${target}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
((failed++))
continue
;;
esac
# actions on error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
done
# actions on error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
_pack_zip()
{
zip -9 -r "${@}"
zip -9 -r "${@}"
}
_pack_tgz()
{
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}"
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}"
}
_pack_txz()
{
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}"
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}"
}
_pack_tar()
{
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}"
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}"
}
_pack_gz()
{
pv "${2}" | gzip -1 > "${1}"
pv "${2}" | gzip -1 > "${1}"
}
_pack_xz()
{
pv "${2}" | xz -9e > "${1}"
pv "${2}" | xz -9e > "${1}"
}
_unpack_zip()
{
unzip "${1}"
unzip "${1}"
}
_unpack_7z()
{
7za x "${1}"
7za x "${1}"
}
_unpack_tgz()
{
pv "${1}" | gzip -d | tar -xf -
pv "${1}" | gzip -d | tar -xf -
}
_unpack_txz()
{
pv "${1}" | xz -d | tar -xf -
pv "${1}" | xz -d | tar -xf -
}
_unpack_tar()
{
pv "${1}" | tar -xf -
pv "${1}" | tar -xf -
}
_unpack_iso()
{
7za x "${1}"
7za x "${1}"
}
_unpack_rar()
{
unrar x "${1}"
unrar x "${1}"
}
_unpack_gz()
{
pv "${1}" | gzip -d > "${1%.gz}"
pv "${1}" | gzip -d > "${1%.gz}"
}
_unpack_xz()
{
pv "${1}" | xz -d > "${1%.xz}"
pv "${1}" | xz -d > "${1%.xz}"
}
# export functions.

View file

@ -2,35 +2,35 @@
# usage: parse_simplify <STRING>
parse_simplify()
{
echo "${*}" | \
sed -e "s/ /_/g" \
-e "s/[^[:alnum:]_-]//g" \
-e "s/_\+/_/g" -e "s/-\+/-/g" \
-e "s/_-/_/g" -e "s/-_/_/g" \
-e "s/_\+/_/g" \
-e "s/^_//" -e "s/_$//"
echo "${*}" | \
sed -e "s/ /_/g" \
-e "s/[^[:alnum:]_-]//g" \
-e "s/_\+/_/g" -e "s/-\+/-/g" \
-e "s/_-/_/g" -e "s/-_/_/g" \
-e "s/_\+/_/g" \
-e "s/^_//" -e "s/_$//"
}
# Parse to CamelCase.
# Usage: parse_camel <STRING>
parse_camel()
{
local IFS=${IFS}_-
local parts=($(parse_simplify ${1}))
local result
local IFS=${IFS}_-
local parts=($(parse_simplify ${1}))
local result
for part in "${parts[@]}"; do
local word="${part^}"
result="${result}${word}"
done
for part in "${parts[@]}"; do
local word="${part^}"
result="${result}${word}"
done
echo "${result}"
echo "${result}"
}
# parse data keeping only alphanumeric characters.
# usage: parse_alnum <STRING>
parse_alnum()
{
echo "${*}" | \
sed -e "s/[^[:alnum:]]//g"
echo "${*}" | \
sed -e "s/[^[:alnum:]]//g"
}

View file

@ -3,125 +3,125 @@ PROMPT_COMMAND=(__prompt_command "${PROMPT_COMMAND[@]}")
# custom terminal prompt format.
__prompt_command()
{
local last_status="${?}"
local is_error=false
local is_root=false
local last_status="${?}"
local is_error=false
local is_root=false
if [[ $last_status != 0 && $last_status != 130 ]]; then
is_error=true
fi
if [[ "$UID" -eq 0 ]]; then
is_root=true
fi
if [[ $last_status != 0 && $last_status != 130 ]]; then
is_error=true
fi
if [[ "$UID" -eq 0 ]]; then
is_root=true
fi
# add newline
PS1="\n"
# add newline
PS1="\n"
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="["
else
PS1+="\[${color_default}\]"
PS1+="["
fi
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="["
else
PS1+="\[${color_default}\]"
PS1+="["
fi
# add time
PS1+="\[${color_white}\]"
PS1+="$(date +%H:%M) "
# add time
PS1+="\[${color_white}\]"
PS1+="$(date +%H:%M) "
# set root red
if $is_root; then
PS1+="\[${color_red}\]"
else
PS1+="\[${color_cyan}\]"
fi
# set root red
if $is_root; then
PS1+="\[${color_red}\]"
else
PS1+="\[${color_cyan}\]"
fi
# add user, host and working dir
PS1+="\u@\h "
PS1+="\[${color_blue}\]"
PS1+="\w"
# PS1+="\${PWD}"
# add user, host and working dir
PS1+="\u@\h "
PS1+="\[${color_blue}\]"
PS1+="\w"
# PS1+="\${PWD}"
# Add git branch if available.
if ! _git_is_dotfiles; then
local git_branch="$(_git_current_branch)"
if [[ "${git_branch}" != "" ]]; then
PS1+=" ${color_bblue}@${git_branch}"
fi
fi
# Add git branch if available.
if ! _git_is_dotfiles; then
local git_branch="$(_git_current_branch)"
if [[ "${git_branch}" != "" ]]; then
PS1+=" ${color_bblue}@${git_branch}"
fi
fi
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="] "
else
PS1+="\[${color_default}\]"
PS1+="] "
fi
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="] "
else
PS1+="\[${color_default}\]"
PS1+="] "
fi
# If error, show code.
if ${is_error}; then
PS1+="${color_red}("
PS1+="${last_status}"
local error_type="$(_ps1error ${last_status})"
[[ "${error_type}" != "" ]] && PS1+=" ${error_type}"
PS1+=")${color_default} "
fi
# If error, show code.
if ${is_error}; then
PS1+="${color_red}("
PS1+="${last_status}"
local error_type="$(_ps1error ${last_status})"
[[ "${error_type}" != "" ]] && PS1+=" ${error_type}"
PS1+=")${color_default} "
fi
# command on new line
PS1+="\n"
PS1+="\[${color_default}\]"
# command on new line
PS1+="\n"
PS1+="\[${color_default}\]"
# set user tag
if $is_root; then
PS1+="# "
else
PS1+="$ "
fi
# set user tag
if $is_root; then
PS1+="# "
else
PS1+="$ "
fi
}
_ps1error()
{
local type
case ${1} in
1) type="GENERAL" ;;
2) type="MISUSE" ;;
126) type="CANTEXEC" ;;
127) type="CMDNF" ;;
129) type="SIGHUP" ;;
130) type="SIGINT" ;;
131) type="SIGQUIT" ;;
132) type="SIGILL" ;;
133) type="SIGTRAP" ;;
134) type="SIGABRT" ;;
135) type="SIGBUS" ;;
136) type="SIGFPE" ;;
137) type="SIGKILL" ;;
138) type="SIGUSR1" ;;
139) type="SIGSEGV" ;;
140) type="SIGUSR2" ;;
141) type="SIGPIPE" ;;
142) type="SIGALRM" ;;
143) type="SIGTERM" ;;
144) type="" ;;
145) type="SIGCHLD" ;;
146) type="SIGCONT" ;;
147) type="SIGSTOP" ;;
148) type="SIGTSTP" ;;
149) type="SIGTTIN" ;;
150) type="SIGTTOU" ;;
151) type="SIGURG" ;;
152) type="SIGXCPU" ;;
153) type="SIGXFSZ" ;;
154) type="SIGVTALRM" ;;
155) type="SIGPROF" ;;
156) type="SIGWINCH" ;;
157) type="SIGIO" ;;
158) type="SIGPWR" ;;
159) type="SIGSYS" ;;
*) type="" ;;
esac
local type
case ${1} in
1) type="GENERAL" ;;
2) type="MISUSE" ;;
126) type="CANTEXEC" ;;
127) type="CMDNF" ;;
129) type="SIGHUP" ;;
130) type="SIGINT" ;;
131) type="SIGQUIT" ;;
132) type="SIGILL" ;;
133) type="SIGTRAP" ;;
134) type="SIGABRT" ;;
135) type="SIGBUS" ;;
136) type="SIGFPE" ;;
137) type="SIGKILL" ;;
138) type="SIGUSR1" ;;
139) type="SIGSEGV" ;;
140) type="SIGUSR2" ;;
141) type="SIGPIPE" ;;
142) type="SIGALRM" ;;
143) type="SIGTERM" ;;
144) type="" ;;
145) type="SIGCHLD" ;;
146) type="SIGCONT" ;;
147) type="SIGSTOP" ;;
148) type="SIGTSTP" ;;
149) type="SIGTTIN" ;;
150) type="SIGTTOU" ;;
151) type="SIGURG" ;;
152) type="SIGXCPU" ;;
153) type="SIGXFSZ" ;;
154) type="SIGVTALRM" ;;
155) type="SIGPROF" ;;
156) type="SIGWINCH" ;;
157) type="SIGIO" ;;
158) type="SIGPWR" ;;
159) type="SIGSYS" ;;
*) type="" ;;
esac
echo -n "${type}"
echo -n "${type}"
}

View file

@ -2,64 +2,64 @@
# Usage: recursive <COMMAND>
recursive()
{
local IFS=$'\n'
local current="${PWD}"
local dirs=$(find -type d)
local total=$(find -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0
local failed=0
local IFS=$'\n'
local current="${PWD}"
local dirs=$(find -type d)
local total=$(find -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0
local failed=0
for dir in ${dirs}; do
# increment counter.
((count++))
for dir in ${dirs}; do
# increment counter.
((count++))
# cd into the next dir.
cd "${current}" || failed=${?}
cd "${dir}" || failed=${?}
# cd into the next dir.
cd "${current}" || failed=${?}
cd "${dir}" || failed=${?}
# echo status.
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
# echo status.
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
# run command.
$* || failed=${?}
done
# run command.
$* || failed=${?}
done
# return back on complete.
cd "${current}" || failed=${?}
# return back on complete.
cd "${current}" || failed=${?}
return ${failed}
return ${failed}
}
# Run something recursively over all directories.
# Usage: recursive1 <COMMAND>
recursive1() # TODO: create generic function.
{
local IFS=$'\n'
local current="${PWD}"
local dirs=$(find -mindepth 1 -maxdepth 1 -type d)
local total=$(find -mindepth 1 -maxdepth 1 -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0
local failed=0
local IFS=$'\n'
local current="${PWD}"
local dirs=$(find -mindepth 1 -maxdepth 1 -type d)
local total=$(find -mindepth 1 -maxdepth 1 -type d | wc -l) # TODO: don't call find twice. won't work with "echo ${dirs}".
local count=0
local failed=0
for dir in ${dirs}; do
# increment counter.
((count++))
for dir in ${dirs}; do
# increment counter.
((count++))
# cd into the next dir.
cd "${current}"
cd "${dir}"
# cd into the next dir.
cd "${current}"
cd "${dir}"
# echo status.
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
# echo status.
echo -e "${color_bblue}[${count}/${total}] ${dir}${color_default}"
# run command.
$* || failed=${?}
done
# run command.
$* || failed=${?}
done
# return back on complete.
cd "${current}"
# return back on complete.
cd "${current}"
return ${failed}
return ${failed}
}
# autocomplete.

View file

@ -1,6 +1,6 @@
tsize()
{
local width=$(tput cols)
local height=$(tput lines)
echo "${width}x${height}"
local width=$(tput cols)
local height=$(tput lines)
echo "${width}x${height}"
}

View file

@ -1,17 +1,17 @@
# CD into host's primary tmp dir.
tmp()
{
local host="${HOSTNAME}"
local tmp_path
local host="${HOSTNAME}"
local tmp_path
case "${host}" in
"desktop"|"home")
tmp_path="/var/mnt/storage/hot/tmp"
;;
*)
tmp_path="${HOME}/tmp"
;;
esac
case "${host}" in
"desktop"|"home")
tmp_path="/var/mnt/storage/hot/tmp"
;;
*)
tmp_path="${HOME}/tmp"
;;
esac
cd "${tmp_path}"
cd "${tmp_path}"
}

View file

@ -1,15 +1,15 @@
# create/attach to named session.
ta()
{
local name="$1"
local name="$1"
# set default name.
if [[ "$name" = "" ]]; then
name="main"
fi
# set default name.
if [[ "$name" = "" ]]; then
name="main"
fi
# call tmux.
tmux new -s "$name" 2> /dev/null || tmux attach-session -t "$name"
# call tmux.
tmux new -s "$name" 2> /dev/null || tmux attach-session -t "$name"
}
# detach.
@ -22,62 +22,62 @@ alias tl="tmux list-sessions"
# Usage: trn [NAME]
trn()
{
local name="${1}"
local name="${1}"
[[ "${name}" = "" ]] && name="${PWD##*/}"
[[ "${name}" = "" ]] && name="${PWD##*/}"
tmux rename-session "${name}"
tmux rename-session "${name}"
}
# Assign name (to window). Uses current dir name by default.
# Usage: tn [NAME]
tn()
{
local name="${1}"
local name="${1}"
[[ "${name}" = "" ]] && name="${PWD##*/}"
[[ "${name}" = "" ]] && name="${PWD##*/}"
tmux rename-window "${name}"
tmux rename-window "${name}"
}
# kill specified session or default one.
tk()
{
# set default name.
if [[ "${1}" = "" ]]; then
1="main"
fi
# set default name.
if [[ "${1}" = "" ]]; then
1="main"
fi
# call tmux.
for name in "$@"; do
tmux kill-session -t "${name}"
done
# call tmux.
for name in "$@"; do
tmux kill-session -t "${name}"
done
}
# kill all sessions.
tka()
{
local sessions=$(tmux list-sessions | sed -e 's/:.*//')
local sessions=$(tmux list-sessions | sed -e 's/:.*//')
for session in $sessions; do
tmux kill-session -t "$session"
done
for session in $sessions; do
tmux kill-session -t "$session"
done
}
# autocompletes.
_complete_tmux_session()
{
_autocomplete_first "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
_autocomplete_first "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
}
_complete_tmux_sessions()
{
_autocomplete "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
_autocomplete "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
}
_complete_tmux_name()
{
_autocomplete_first "${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//')
_autocomplete_first "${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//')
}
complete -F _complete_tmux_session ta

View file

@ -2,69 +2,69 @@
# usage: tb [NAME]
tba()
{
local name="${1}"
local name="${1}"
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# start container or run command inside it if specified.
if [[ "${2}" = "" ]]; then
# set hostname.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# start container or run command inside it if specified.
if [[ "${2}" = "" ]]; then
# set hostname.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# try to enter or create if failed.
toolbox enter "${name}" || {
# create container.
toolbox create "${name}"
# try to enter or create if failed.
toolbox enter "${name}" || {
# create container.
toolbox create "${name}"
# initialize container.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# toolbox --container "$name" run sudo rm /root/.bashrc
# toolbox --container "$name" run sudo ln -s $HOME/.linux /root/.linux
# toolbox --container "$name" run sudo ln -s /root/.linux/bash/bashrc.sh /root/.bashrc
# initialize container.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# toolbox --container "$name" run sudo rm /root/.bashrc
# toolbox --container "$name" run sudo ln -s $HOME/.linux /root/.linux
# toolbox --container "$name" run sudo ln -s /root/.linux/bash/bashrc.sh /root/.bashrc
# enter container, finally.
toolbox enter "${name}"
}
else
# set hostname.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# enter container, finally.
toolbox enter "${name}"
}
else
# set hostname.
toolbox --container "${name}" run sudo hostname "${HOSTNAME}-${name}"
# run command inside container.
toolbox --container "${name}" run ${@:2}
fi
# run command inside container.
toolbox --container "${name}" run ${@:2}
fi
}
# remove toolbx container with default or specified name.
# usage: tbk [NAME]
tbk()
{
local name="${1}"
local name="${1}"
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# stop and remove podman container.
podman stop "${name}" > /dev/null && podman rm "${name}" > /dev/null
# stop and remove podman container.
podman stop "${name}" > /dev/null && podman rm "${name}" > /dev/null
}
# install rpm-fusion repository.
# usage: tb_rpmfusion [NAME]
tb_rpmfusion()
{
local name="${1}"
local name="${1}"
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# set default name.
if [[ "${name}" = "" ]]; then
name="main"
fi
# run dnf inside container.
toolbox --container "${name}" run sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
# run dnf inside container.
toolbox --container "${name}" run sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
}
# list all containers.
@ -73,7 +73,7 @@ alias tbl="toolbox list -c"
# autocomplete.
_tb_containers()
{
_autocomplete_first "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')"
_autocomplete_first "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')"
}
complete -F _tb_containers tba tbk tb_rpmfusion

View file

@ -2,133 +2,133 @@
# Usage: transcode <FORMAT> [FILES]
transcode()
{
local IFS=$'\n'
local targets=("${@:2}")
local format="${1}"
local total=${#targets[@]}
local count=0
local failed=0
local skipped=0
local IFS=$'\n'
local targets=("${@:2}")
local format="${1}"
local total=${#targets[@]}
local count=0
local failed=0
local skipped=0
# Report no format.
if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: transcode <FORMAT> [FILES]"
return 2
fi
# Report no format.
if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: transcode <FORMAT> [FILES]"
return 2
fi
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /$))
total=${#targets[@]}
fi
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /$))
total=${#targets[@]}
fi
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Process.
for target in "${targets[@]}"; do
# Increment counter.
((count++))
# Define context names and status.
local from="${target##*.}"
local to="${format}"
local output="${target##*/}"
output="${output%.*}.${to}"
local status="[${count}/${total}] ${target} -> ${output}"
# Define context names and status.
local from="${target##*.}"
local to="${format}"
local output="${target##*/}"
output="${output%.*}.${to}"
local status="[${count}/${total}] ${target} -> ${output}"
# Show status.
echo -e "${status}"
# Show status.
echo -e "${status}"
# Skip if file exists.
[[ -f "${output}" ]] && { ((skipped++)); continue; }
# Skip if file exists.
[[ -f "${output}" ]] && { ((skipped++)); continue; }
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
[[ "${to}" = "flac" ]] && from=""
[[ "${to}" = "mka" ]] && from=""
[[ "${to}" = "mkv" ]] && from=""
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
[[ "${to}" = "flac" ]] && from=""
[[ "${to}" = "mka" ]] && from=""
[[ "${to}" = "mkv" ]] && from=""
# Send convert.
case "${from}-${to}" in
"gz-xz"|"tgz-txz")
_transcode_gz-xz "${target}" "${output}"
;;
"xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}" "${output}"
;;
"-mp3")
_transcode_mp3 "${target}" "${output}"
;;
"-flac")
_transcode_flac "${target}" "${output}"
;;
"-mka")
_transcode_mka "${target}" "${output}"
;;
"-mkv")
_transcode_mkv "${target}" "${output}"
;;
*)
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
false
;;
esac
# Send convert.
case "${from}-${to}" in
"gz-xz"|"tgz-txz")
_transcode_gz-xz "${target}" "${output}"
;;
"xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}" "${output}"
;;
"-mp3")
_transcode_mp3 "${target}" "${output}"
;;
"-flac")
_transcode_flac "${target}" "${output}"
;;
"-mka")
_transcode_mka "${target}" "${output}"
;;
"-mkv")
_transcode_mkv "${target}" "${output}"
;;
*)
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
false
;;
esac
# Show error.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
# Show error.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1
fi
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
_transcode_gz-xz()
{
[[ -f "${2}" ]] && return 1
pv "${1}" | gzip -d | xz -9e > "${2}"
[[ -f "${2}" ]] && return 1
pv "${1}" | gzip -d | xz -9e > "${2}"
}
_transcode_xz-gz()
{
[[ -f "${2}" ]] && return 1
pv "${1}" | xz -d | gzip -1 > "${2}"
[[ -f "${2}" ]] && return 1
pv "${1}" | xz -d | gzip -1 > "${2}"
}
_transcode_mp3()
{
ffmpeg -n -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}"
ffmpeg -n -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}"
}
_transcode_flac()
{
ffmpeg -n -i "${1}" -c:a flac -f flac "${2}"
ffmpeg -n -i "${1}" -c:a flac -f flac "${2}"
}
_transcode_mka()
{
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
ffmpeg -n -i "${1}" -ac 2 -c:a libopus -b:a ${braudio}k -vn "${2}"
ffmpeg -n -i "${1}" -ac 2 -c:a libopus -b:a ${braudio}k -vn "${2}"
}
_transcode_mkv()
{
local keyint=$(_ffprobe_keyint "${1}")
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
local keyint=$(_ffprobe_keyint "${1}")
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
# ffmpeg -n -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 8 -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
ffmpeg -n -i "${1}" -map 0 -map -v -map V -map -t -c:s srt -ac 2 -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
# ffmpeg -n -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 8 -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
ffmpeg -n -i "${1}" -map 0 -map -v -map V -map -t -c:s srt -ac 2 -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
}
# Export.

View file

@ -1,15 +1,15 @@
# retry command every 2 sec until it completes.
try()
{
local result=-1
local result=-1
while [ "$result" != 0 ]; do
$@
result=$?
if [ "$result" != 0 ]; then
sleep 2
fi
done
while [ "$result" != 0 ]; do
$@
result=$?
if [ "$result" != 0 ]; then
sleep 2
fi
done
}
# autocomplete.

View file

@ -1,5 +1,5 @@
# Get the number of avaialble cores (threads).
_core_count()
{
cat /proc/cpuinfo | grep ^processor | wc -l
cat /proc/cpuinfo | grep ^processor | wc -l
}

View file

@ -2,40 +2,40 @@
# Usage: vdl [LINK]
vdl()
{
# Check that ffmpeg and ffprobe are available.
if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then
echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}"
return 1
fi
# Check that ffmpeg and ffprobe are available.
if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then
echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}"
return 1
fi
local target="${@}" # What to download/update.
local target="${@}" # What to download/update.
# If no [LINK] provided, try to read from `src.txt`.
[[ "${target}" = "" ]] && target="$(cat src.txt)"
# If no [LINK] provided, try to read from `src.txt`.
[[ "${target}" = "" ]] && target="$(cat src.txt)"
# If could not get [LINK] eventually, show an error and exit.
if [[ "${target}" = "" ]]; then
echo -e "${color_red}Could not determine [LINK] to download.${color_default}"
echo "Usage: vdl [LINK]"
return 2
fi
# If could not get [LINK] eventually, show an error and exit.
if [[ "${target}" = "" ]]; then
echo -e "${color_red}Could not determine [LINK] to download.${color_default}"
echo "Usage: vdl [LINK]"
return 2
fi
# Save [LINK] for later use.
[[ -f "src.txt" ]] || echo "${target}" > src.txt
# Save [LINK] for later use.
[[ -f "src.txt" ]] || echo "${target}" > src.txt
# Download [LINK] content.
yt-dlp -f 'bestvideo[height<=?1081]+bestaudio/best' --download-archive index.txt --embed-thumbnail --embed-subs --write-auto-subs --embed-metadata --merge-output-format mkv -cio '%(playlist_index)000006d_%(id)s.%(ext)s' ${target} # || _vdl_retry
# Download [LINK] content.
yt-dlp -f 'bestvideo[height<=?1081]+bestaudio/best' --download-archive index.txt --embed-thumbnail --embed-subs --write-auto-subs --embed-metadata --merge-output-format mkv -cio '%(playlist_index)000006d_%(id)s.%(ext)s' ${target} # || _vdl_retry
}
alias vdl_vk="vdl --format mp4"
# Temporary fix for crashes.
_vdl_retry() {
for file in *.part; do
local number="${file%%_*}"
rm ${number}*
done
vdl
for file in *.part; do
local number="${file%%_*}"
rm ${number}*
done
vdl
}
# download all videos from file.

View file

@ -1,7 +1,7 @@
# set specified file as a wallpaper.
wallpaper()
{
path_wallpaper=~/.local/share/backgrounds/background.jpg
cp "$1" $path_wallpaper
chmod 644 $path_wallpaper
path_wallpaper=~/.local/share/backgrounds/background.jpg
cp "$1" $path_wallpaper
chmod 644 $path_wallpaper
}