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

View file

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

View file

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

View file

@ -2,132 +2,132 @@
# Usage: checksum_create [FILES] # Usage: checksum_create [FILES]
checksum_create() checksum_create()
{ {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local total=${#} local total=${#}
local failed=0 local failed=0
# All files by default. # All files by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$)) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# Iterate each file. # Iterate each file.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1" local hashfile=".${target#./}.sha1"
# Increment count. # Increment count.
((count++)) ((count++))
# Status info. # Status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo -e "${status}" echo -e "${status}"
# Skip if hash exists. # Skip if hash exists.
[[ -f "${hashfile}" ]] && continue [[ -f "${hashfile}" ]] && continue
# Calculate hash. # Calculate hash.
pv "${target}" | sha1sum > ${hashfile} pv "${target}" | sha1sum > ${hashfile}
# Report success. # Report success.
if [[ ${?} != 0 ]]; then if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
if [[ ${failed} != 0 ]]; then if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}" echo -e "${color_bred}Failed: ${failed}.${color_default}"
return 1 return 1
fi fi
[[ ${total} = 0 ]] && return 1 [[ ${total} = 0 ]] && return 1
} }
# Check stored values against actual files. # Check stored values against actual files.
checksum_check() checksum_check()
{ {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local total=${#} local total=${#}
local failed=0 local failed=0
local skipped=0 local skipped=0
# All files by default. # All files by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /\$)) targets=($(ls --classify | grep -v /\$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# Iterate each file. # Iterate each file.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
local hashfile=".${target#./}.sha1" local hashfile=".${target#./}.sha1"
# Increment count. # Increment count.
((count++)) ((count++))
# Status info. # Status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo -e "${status}" echo -e "${status}"
# Skip if hash doesn't exist. # Skip if hash doesn't exist.
[[ -f "${hashfile}" ]] || { ((skipped++)); continue; } [[ -f "${hashfile}" ]] || { ((skipped++)); continue; }
# Calculate hash. # Calculate hash.
local stored=$(cat "${hashfile}" | cut -d\ -f1) local stored=$(cat "${hashfile}" | cut -d\ -f1)
local actual=$(pv "${target}" | sha1sum | cut -d\ -f1) local actual=$(pv "${target}" | sha1sum | cut -d\ -f1)
if [[ "${stored}" != "${actual}" ]]; then if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
if [[ ${skipped} != 0 ]]; then if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}" echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1 return 1
fi fi
if [[ ${failed} != 0 ]]; then if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}" echo -e "${color_bred}Failed: ${failed}.${color_default}"
return 1 return 1
fi fi
} }
checksum() checksum()
{ {
find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1 find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1
} }
_checksum_create() _checksum_create()
{ {
local path="${1%/*}" local path="${1%/*}"
local name="${1##*/}" local name="${1##*/}"
sha1sum "${path}/${name}" > "${path}/.${name}.sha1" sha1sum "${path}/${name}" > "${path}/.${name}.sha1"
} }
_checksum_check() _checksum_check()
{ {
local file="${1##*\ \ }" local file="${1##*\ \ }"
local stored="${1%%\ \ *}" local stored="${1%%\ \ *}"
# Skip if no file. # Skip if no file.
[[ -f "${file}" ]] || return 0 [[ -f "${file}" ]] || return 0
# Check file hash. # Check file hash.
local actual=$(sha1sum "${file}") local actual=$(sha1sum "${file}")
actual="${actual%%\ \ *}" actual="${actual%%\ \ *}"
# Compare values. # Compare values.
if [[ "${stored}" != "${actual}" ]]; then if [[ "${stored}" != "${actual}" ]]; then
echo -e "${color_bred}${file}: Failed.${color_default}" echo -e "${color_bred}${file}: Failed.${color_default}"
return 1 return 1
fi fi
return 0 return 0
} }
export -f checksum_create checksum_check 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. # print all available colors with their names colored in corresponding color.
color_test() color_test()
{ {
echo -e " ${color_default}color_default\n\ echo -e " ${color_default}color_default\n\
${color_blue}color_blue\n\ ${color_blue}color_blue\n\
${color_bblue}color_bblue\n\ ${color_bblue}color_bblue\n\
${color_cyan}color_cyan\n\ ${color_cyan}color_cyan\n\
${color_bcyan}color_bcyan\n\ ${color_bcyan}color_bcyan\n\
${color_green}color_green\n\ ${color_green}color_green\n\
${color_bgreen}color_bgreen\n\ ${color_bgreen}color_bgreen\n\
${color_purple}color_purple\n\ ${color_purple}color_purple\n\
${color_bpurple}color_bpurple\n\ ${color_bpurple}color_bpurple\n\
${color_red}color_red\n\ ${color_red}color_red\n\
${color_bred}color_bred\n\ ${color_bred}color_bred\n\
${color_white}color_white\n\ ${color_white}color_white\n\
${color_bwhite}color_bwhite\n\ ${color_bwhite}color_bwhite\n\
${color_yellow}color_yellow\n\ ${color_yellow}color_yellow\n\
${color_byellow}color_byellow" ${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. alias paste="wl-paste" # paste system clipboard to stdout.

View file

@ -1,4 +1,4 @@
_GDCONF_PATH="${HOME}/.config/linux/gnome.dconf" _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. 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] # usage: dcdu [SERVICES]
dcdu() dcdu()
{ {
dcd "${@}" dcd "${@}"
dcu "${@}" dcu "${@}"
} }
# pull & up specified services. # pull & up specified services.
# usage: dcpu [SERVICES] # usage: dcpu [SERVICES]
dcpu() dcpu()
{ {
dcp "${@}" dcp "${@}"
dcu "${@}" dcu "${@}"
} }
# up & attach to logs for specified services. # up & attach to logs for specified services.
# usage: dcul [SERVICES] # usage: dcul [SERVICES]
dcul() dcul()
{ {
dcu "${@}" && dcl "${@}" dcu "${@}" && dcl "${@}"
} }
# find out container's IP address. # find out container's IP address.
# usage: docker_up <CONTAINER NAME> # usage: docker_up <CONTAINER NAME>
docker_ip() 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. # update all docker images.
docker_update() docker_update()
{ {
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull
} }
# autocomplete. # autocomplete.
_dc_services() _dc_services()
{ {
_autocomplete "$(docker compose config --services 2> /dev/null)" _autocomplete "$(docker compose config --services 2> /dev/null)"
} }
_dc_containers() _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 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> # usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>
ffmpeg_mux_audio() ffmpeg_mux_audio()
{ {
if [[ "$1" = "" ]]; then if [[ "$1" = "" ]]; then
echo "usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>" echo "usage: ffmpeg_mux_audio <SOUND> <OUTPUT DIR>"
return 2 return 2
fi 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. # Mux cover into music file.
# Usage: ffmpeg_mux_cover <FORMAT> <COVER> # Usage: ffmpeg_mux_cover <FORMAT> <COVER>
ffmpeg_mux_cover() ffmpeg_mux_cover()
{ {
if [[ "${1}" = "" ]]; then if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_mux_cover <FORMAT> <COVER>" echo "Usage: ffmpeg_mux_cover <FORMAT> <COVER>"
return 2 return 2
fi fi
local format="${1}" local format="${1}"
local cover="${2}" local cover="${2}"
mkdir out mkdir out
case "${format}" in case "${format}" in
# "mka"|"mkv") # "mka"|"mkv")
# for file in *.${format}; do # 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 # 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 # done
# ;; # ;;
*) *)
for file in *.${format}; do 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 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 done
;; ;;
esac esac
mv out/* . mv out/* .
rm -d out/ && rm "${2}" rm -d out/ && rm "${2}"
} }
# Change music metadata. # Change music metadata.
# Usage: ffmpeg_music_meta <FORMAT> # Usage: ffmpeg_music_meta <FORMAT>
ffmpeg_music_meta() ffmpeg_music_meta()
{ {
if [[ "${1}" = "" ]]; then if [[ "${1}" = "" ]]; then
echo "Usage: ffmpeg_music_meta <FORMAT>" echo "Usage: ffmpeg_music_meta <FORMAT>"
return 2 return 2
fi 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 artist="${PWD%/*}"; artist="${artist##*/}"; artist="${artist//_/ }"
local album="${PWD##*/}"; album="${album#*_}"; album="${album//_/ }" local album="${PWD##*/}"; album="${album#*_}"; album="${album//_/ }"
local year="${PWD##*/}"; year="${year%%_*}" local year="${PWD##*/}"; year="${year%%_*}"
# local total=$(ls *.${format} | wc -l) # local total=$(ls *.${format} | wc -l)
mkdir out mkdir out
for file in *.${format}; do for file in *.${format}; do
local track="${file%%_*}"; track=$((10#${track})); [[ "${track}" = "" ]] && track=0 local track="${file%%_*}"; track=$((10#${track})); [[ "${track}" = "" ]] && track=0
local title="${file#*_}"; title="${title%.*}"; title="${title//_/ }" local title="${file#*_}"; title="${title%.*}"; title="${title//_/ }"
# echo "${artist}; ${album}; ${year}; ${track}; ${title}" # echo "${artist}; ${album}; ${year}; ${track}; ${title}"
# TODO: make it format-specific. # 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 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 done
mv out/* . mv out/* .
rm -d out/ rm -d out/
} }
_ffprobe_fps() _ffprobe_fps()
{ {
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}") local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}")
fps="${fps%%/*}" fps="${fps%%/*}"
echo "${fps}" echo "${fps}"
} }
_ffprobe_keyint() _ffprobe_keyint()
{ {
local fps=$(_ffprobe_fps "${1}") local fps=$(_ffprobe_fps "${1}")
echo $((fps*5)) echo $((fps*5))
} }
_ffprobe_ba() _ffprobe_ba()
{ {
local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}") 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 [[ "${ba}" != "N/A" ]] && echo $((ba/1024)) || echo 128
} }
export -f _ffprobe_keyint _ffprobe_ba _ffprobe_fps export -f _ffprobe_keyint _ffprobe_ba _ffprobe_fps

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,11 +1,11 @@
# send Telegram notification. # send Telegram notification.
notify() 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. # send silent Telegram notification.
notify_silent() 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] # usage: own [USER] [FILES]
own() own()
{ {
local file="${2}" local file="${2}"
local user="${1}" local user="${1}"
# default to current dir. # default to current dir.
if [ "${file}" = "" ]; then if [ "${file}" = "" ]; then
file='.' file='.'
fi fi
# default to current user. # default to current user.
if [ "${user}" = "" ]; then if [ "${user}" = "" ]; then
user="${UID}" user="${UID}"
fi fi
# set ownership. # set ownership.
chown "${user}":"${user}" -R "${file}" &> /dev/null chown "${user}":"${user}" -R "${file}" &> /dev/null
# remove access from group and others. # remove access from group and others.
chmod -077 -R "${file}" 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] # Usage: pack <TARGET.ext> [FILES]
pack() pack()
{ {
local IFS=$'\n' local IFS=$'\n'
local output="${1}" local output="${1}"
local targets=("${@:2}") local targets=("${@:2}")
local format="${output##*.}" local format="${output##*.}"
local name="${output%.*}" local name="${output%.*}"
# report no output. # report no output.
if [[ "${output}" = "" ]]; then if [[ "${output}" = "" ]]; then
echo "Usage: pack <TARGET.ext> [FILES]" echo "Usage: pack <TARGET.ext> [FILES]"
return 2 return 2
fi fi
# report no format. # report no format.
if [[ "${format}" = "" ]]; then if [[ "${format}" = "" ]]; then
echo "Could not determine output format." echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> [FILES]" echo "Usage: pack <TARGET.ext> [FILES]"
return 2 return 2
fi fi
# All targets by default. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls)) targets=($(ls))
fi fi
# process. # process.
case "${format}" in case "${format}" in
"tgz") "tgz")
_pack_tgz "${output}" "${targets[@]}" _pack_tgz "${output}" "${targets[@]}"
;; ;;
"txz") "txz")
_pack_txz "${output}" "${targets[@]}" _pack_txz "${output}" "${targets[@]}"
;; ;;
"tar") "tar")
_pack_tar "${output}" "${targets[@]}" _pack_tar "${output}" "${targets[@]}"
;; ;;
"zip") "zip")
_pack_zip "${output}" "${targets[@]}" _pack_zip "${output}" "${targets[@]}"
;; ;;
"gz") "gz")
_pack_gz "${output}" "${targets[@]}" _pack_gz "${output}" "${targets[@]}"
;; ;;
"xz") "xz")
_pack_xz "${output}" "${targets[@]}" _pack_xz "${output}" "${targets[@]}"
;; ;;
*) *)
echo -e "${color_bred}${target}: Format not supported.${color_default}" echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1 return 1
;; ;;
esac esac
# Show error. # Show error.
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}" echo -e "${color_bred}${target}: Failed.${color_default}"
fi fi
} }
# attempt to unpack everything. # attempt to unpack everything.
# usage: unpack [FILES] # usage: unpack [FILES]
unpack() unpack()
{ {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@}") local targets=("${@}")
local count=0 local count=0
local total=${#targets[@]} local total=${#targets[@]}
local failed=0 local failed=0
# All targets by default. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_UNPACK_SUPPORTED})) targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
total=${#targets[@]} total=${#targets[@]}
fi fi
# process all files. # process all files.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# increment counter. # increment counter.
((count++)) ((count++))
# prepare status. # prepare status.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
# show status. # show status.
echo -e "${status}" echo -e "${status}"
# unpack file type. # unpack file type.
local type="${target##*.}" local type="${target##*.}"
[[ "${target}" =~ .tar.gz$ ]] && type="tar.gz" [[ "${target}" =~ .tar.gz$ ]] && type="tar.gz"
[[ "${target}" =~ .tar.xz$ ]] && type="tar.xz" [[ "${target}" =~ .tar.xz$ ]] && type="tar.xz"
# unpack content. # unpack content.
case "${type,,}" in case "${type,,}" in
"zip") "zip")
_unpack_zip "${target}" _unpack_zip "${target}"
;; ;;
"7z") "7z")
_unpack_7z "${target}" _unpack_7z "${target}"
;; ;;
"tgz"|"tar.gz") "tgz"|"tar.gz")
_unpack_tgz "${target}" _unpack_tgz "${target}"
;; ;;
"txz"|"tar.xz") "txz"|"tar.xz")
_unpack_txz "${target}" _unpack_txz "${target}"
;; ;;
"tar") "tar")
_unpack_tar "${target}" _unpack_tar "${target}"
;; ;;
"iso") "iso")
_unpack_iso "${target}" _unpack_iso "${target}"
;; ;;
"rar") "rar")
_unpack_rar "${target}" _unpack_rar "${target}"
;; ;;
"xz") "xz")
_unpack_xz "${target}" _unpack_xz "${target}"
;; ;;
"gz") "gz")
_unpack_gz "${target}" _unpack_gz "${target}"
;; ;;
*) *)
echo -e "${color_bred}${target}: Format not supported.${color_default}" echo -e "${color_bred}${target}: Format not supported.${color_default}"
((failed++)) ((failed++))
continue continue
;; ;;
esac esac
# actions on error. # actions on error.
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}" echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
if [[ ${failed} != 0 ]]; then if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}" echo -e "${color_bred}Failed: ${failed}.${color_default}"
false false
fi fi
} }
_pack_zip() _pack_zip()
{ {
zip -9 -r "${@}" zip -9 -r "${@}"
} }
_pack_tgz() _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() _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() _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() _pack_gz()
{ {
pv "${2}" | gzip -1 > "${1}" pv "${2}" | gzip -1 > "${1}"
} }
_pack_xz() _pack_xz()
{ {
pv "${2}" | xz -9e > "${1}" pv "${2}" | xz -9e > "${1}"
} }
_unpack_zip() _unpack_zip()
{ {
unzip "${1}" unzip "${1}"
} }
_unpack_7z() _unpack_7z()
{ {
7za x "${1}" 7za x "${1}"
} }
_unpack_tgz() _unpack_tgz()
{ {
pv "${1}" | gzip -d | tar -xf - pv "${1}" | gzip -d | tar -xf -
} }
_unpack_txz() _unpack_txz()
{ {
pv "${1}" | xz -d | tar -xf - pv "${1}" | xz -d | tar -xf -
} }
_unpack_tar() _unpack_tar()
{ {
pv "${1}" | tar -xf - pv "${1}" | tar -xf -
} }
_unpack_iso() _unpack_iso()
{ {
7za x "${1}" 7za x "${1}"
} }
_unpack_rar() _unpack_rar()
{ {
unrar x "${1}" unrar x "${1}"
} }
_unpack_gz() _unpack_gz()
{ {
pv "${1}" | gzip -d > "${1%.gz}" pv "${1}" | gzip -d > "${1%.gz}"
} }
_unpack_xz() _unpack_xz()
{ {
pv "${1}" | xz -d > "${1%.xz}" pv "${1}" | xz -d > "${1%.xz}"
} }
# export functions. # export functions.

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,133 +2,133 @@
# Usage: transcode <FORMAT> [FILES] # Usage: transcode <FORMAT> [FILES]
transcode() transcode()
{ {
local IFS=$'\n' local IFS=$'\n'
local targets=("${@:2}") local targets=("${@:2}")
local format="${1}" local format="${1}"
local total=${#targets[@]} local total=${#targets[@]}
local count=0 local count=0
local failed=0 local failed=0
local skipped=0 local skipped=0
# Report no format. # Report no format.
if [[ "${format}" = "" ]]; then if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}" echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: transcode <FORMAT> [FILES]" echo "Usage: transcode <FORMAT> [FILES]"
return 2 return 2
fi fi
# All files by default. # All files by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /$)) targets=($(ls --classify | grep -v /$))
total=${#targets[@]} total=${#targets[@]}
fi fi
# Process. # Process.
for target in "${targets[@]}"; do for target in "${targets[@]}"; do
# Increment counter. # Increment counter.
((count++)) ((count++))
# Define context names and status. # Define context names and status.
local from="${target##*.}" local from="${target##*.}"
local to="${format}" local to="${format}"
local output="${target##*/}" local output="${target##*/}"
output="${output%.*}.${to}" output="${output%.*}.${to}"
local status="[${count}/${total}] ${target} -> ${output}" local status="[${count}/${total}] ${target} -> ${output}"
# Show status. # Show status.
echo -e "${status}" echo -e "${status}"
# Skip if file exists. # Skip if file exists.
[[ -f "${output}" ]] && { ((skipped++)); continue; } [[ -f "${output}" ]] && { ((skipped++)); continue; }
# Support multiple inputs. # Support multiple inputs.
[[ "${to}" = "mp3" ]] && from="" [[ "${to}" = "mp3" ]] && from=""
[[ "${to}" = "flac" ]] && from="" [[ "${to}" = "flac" ]] && from=""
[[ "${to}" = "mka" ]] && from="" [[ "${to}" = "mka" ]] && from=""
[[ "${to}" = "mkv" ]] && from="" [[ "${to}" = "mkv" ]] && from=""
# Send convert. # Send convert.
case "${from}-${to}" in case "${from}-${to}" in
"gz-xz"|"tgz-txz") "gz-xz"|"tgz-txz")
_transcode_gz-xz "${target}" "${output}" _transcode_gz-xz "${target}" "${output}"
;; ;;
"xz-gz"|"txz-tgz") "xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}" "${output}" _transcode_xz-gz "${target}" "${output}"
;; ;;
"-mp3") "-mp3")
_transcode_mp3 "${target}" "${output}" _transcode_mp3 "${target}" "${output}"
;; ;;
"-flac") "-flac")
_transcode_flac "${target}" "${output}" _transcode_flac "${target}" "${output}"
;; ;;
"-mka") "-mka")
_transcode_mka "${target}" "${output}" _transcode_mka "${target}" "${output}"
;; ;;
"-mkv") "-mkv")
_transcode_mkv "${target}" "${output}" _transcode_mkv "${target}" "${output}"
;; ;;
*) *)
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}" echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
false false
;; ;;
esac esac
# Show error. # Show error.
if [[ ${?} != 0 ]]; then if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}" echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++)) ((failed++))
fi fi
done done
if [[ ${skipped} != 0 ]]; then if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}" echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1 return 1
fi fi
if [[ ${failed} != 0 ]]; then if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}" echo -e "${color_bred}Failed: ${failed}.${color_default}"
false false
fi fi
} }
_transcode_gz-xz() _transcode_gz-xz()
{ {
[[ -f "${2}" ]] && return 1 [[ -f "${2}" ]] && return 1
pv "${1}" | gzip -d | xz -9e > "${2}" pv "${1}" | gzip -d | xz -9e > "${2}"
} }
_transcode_xz-gz() _transcode_xz-gz()
{ {
[[ -f "${2}" ]] && return 1 [[ -f "${2}" ]] && return 1
pv "${1}" | xz -d | gzip -1 > "${2}" pv "${1}" | xz -d | gzip -1 > "${2}"
} }
_transcode_mp3() _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() _transcode_flac()
{ {
ffmpeg -n -i "${1}" -c:a flac -f flac "${2}" ffmpeg -n -i "${1}" -c:a flac -f flac "${2}"
} }
_transcode_mka() _transcode_mka()
{ {
local braudio=$(_ffprobe_ba "${1}") local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128 [[ ${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() _transcode_mkv()
{ {
local keyint=$(_ffprobe_keyint "${1}") local keyint=$(_ffprobe_keyint "${1}")
local braudio=$(_ffprobe_ba "${1}") local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128 [[ ${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}" -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}" -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. # Export.

View file

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

View file

@ -1,5 +1,5 @@
# Get the number of avaialble cores (threads). # Get the number of avaialble cores (threads).
_core_count() _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] # Usage: vdl [LINK]
vdl() vdl()
{ {
# Check that ffmpeg and ffprobe are available. # Check that ffmpeg and ffprobe are available.
if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then
echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}" echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}"
return 1 return 1
fi fi
local target="${@}" # What to download/update. local target="${@}" # What to download/update.
# If no [LINK] provided, try to read from `src.txt`. # If no [LINK] provided, try to read from `src.txt`.
[[ "${target}" = "" ]] && target="$(cat src.txt)" [[ "${target}" = "" ]] && target="$(cat src.txt)"
# If could not get [LINK] eventually, show an error and exit. # If could not get [LINK] eventually, show an error and exit.
if [[ "${target}" = "" ]]; then if [[ "${target}" = "" ]]; then
echo -e "${color_red}Could not determine [LINK] to download.${color_default}" echo -e "${color_red}Could not determine [LINK] to download.${color_default}"
echo "Usage: vdl [LINK]" echo "Usage: vdl [LINK]"
return 2 return 2
fi fi
# Save [LINK] for later use. # Save [LINK] for later use.
[[ -f "src.txt" ]] || echo "${target}" > src.txt [[ -f "src.txt" ]] || echo "${target}" > src.txt
# Download [LINK] content. # 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 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" alias vdl_vk="vdl --format mp4"
# Temporary fix for crashes. # Temporary fix for crashes.
_vdl_retry() { _vdl_retry() {
for file in *.part; do for file in *.part; do
local number="${file%%_*}" local number="${file%%_*}"
rm ${number}* rm ${number}*
done done
vdl vdl
} }
# download all videos from file. # download all videos from file.

View file

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