diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-02 08:18:01 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-02 13:39:59 +0300 |
commit | 42da15c1fbff60c4a49705b96ebb30721ccafb14 (patch) | |
tree | 3b86ccf4fd01e7224b5e401ba14cd49cee78ebae /%HOME%/.bash_utils | |
parent | .bash_utils: remove mysql.sh (diff) | |
download | linux-home-42da15c1fbff60c4a49705b96ebb30721ccafb14.tar.gz linux-home-42da15c1fbff60c4a49705b96ebb30721ccafb14.zip |
bash: best practices & linting
Diffstat (limited to '%HOME%/.bash_utils')
-rw-r--r-- | %HOME%/.bash_utils/alternatives.sh | 6 | ||||
-rw-r--r-- | %HOME%/.bash_utils/cxx.sh | 21 | ||||
-rw-r--r-- | %HOME%/.bash_utils/distr.sh | 49 | ||||
-rw-r--r-- | %HOME%/.bash_utils/file.sh | 1 | ||||
-rw-r--r-- | %HOME%/.bash_utils/git.sh | 46 | ||||
-rw-r--r-- | %HOME%/.bash_utils/os.sh | 39 | ||||
-rw-r--r-- | %HOME%/.bash_utils/path.sh | 20 | ||||
-rw-r--r-- | %HOME%/.bash_utils/text.sh | 19 |
8 files changed, 142 insertions, 59 deletions
diff --git a/%HOME%/.bash_utils/alternatives.sh b/%HOME%/.bash_utils/alternatives.sh index 58fcfc3..3cfb958 100644 --- a/%HOME%/.bash_utils/alternatives.sh +++ b/%HOME%/.bash_utils/alternatives.sh @@ -9,6 +9,7 @@ alias update-my-alternatives='update-alternatives --quiet --altdir ~/.local/etc/ setup_alternatives_cc() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit gcc_cc="$( command -v gcc 2> /dev/null )" || return 0 gcc_cxx="$( command -v g++ 2> /dev/null )" || return 0 @@ -24,6 +25,7 @@ setup_alternatives_cc() ( setup_alternatives() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit command -v update-alternatives > /dev/null 2>&1 || return 0 mkdir -p -- ~/.local/etc/alternatives ~/.local/var/lib/alternatives @@ -35,12 +37,16 @@ setup_alternatives alt_gcc() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + gcc_cc="$( command -v gcc 2> /dev/null )" update-my-alternatives --set cc "$gcc_cc" ) alt_clang() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + clang_cc="$( command -v clang 2> /dev/null )" update-my-alternatives --set cc "$clang_cc" ) diff --git a/%HOME%/.bash_utils/cxx.sh b/%HOME%/.bash_utils/cxx.sh index 3aaa918..d094f3f 100644 --- a/%HOME%/.bash_utils/cxx.sh +++ b/%HOME%/.bash_utils/cxx.sh @@ -7,6 +7,8 @@ _runc_os_is_cygwin() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + local os os="$( uname -o )" test 'Cygwin' = "$os" @@ -38,6 +40,7 @@ _runc_get_absolute_path() { _runc_usage() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local prefix="${FUNCNAME[0]}" [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}" @@ -52,6 +55,7 @@ _runc_usage() ( runc() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -a c_flags=(${runc_flags[@]+"${runc_flags[@]}"}) local -a src_files=() @@ -111,22 +115,22 @@ runc() ( src_files=() local src_file - while IFS= read -d '' -r src_file; do + _runc_get_absolute_path ${_src_files[@]+"${_src_files[@]}"} | while IFS= read -d '' -r src_file; do src_files+=("$src_file") - done < <( _runc_get_absolute_path ${_src_files[@]+"${_src_files[@]}"} ) + done if [ "${#include_dirs[@]}" -gt 0 ]; then local include_dir - while IFS= read -d '' -r include_dir; do + _runc_get_absolute_path ${include_dirs[@]+"${include_dirs[@]}"} | while IFS= read -d '' -r include_dir; do c_flags+=("-I$include_dir") - done < <( _runc_get_absolute_path ${include_dirs[@]+"${include_dirs[@]}"} ) + done fi if [ "${#lib_dirs[@]}" -gt 0 ]; then local lib_dir - while IFS= read -d '' -r lib_dir; do + _runc_get_absolute_path ${lib_dirs[@]+"${lib_dirs[@]}"} | while IFS= read -d '' -r lib_dir; do c_flags+=("-L$lib_dir") - done < <( _runc_get_absolute_path ${lib_dirs[@]+"${lib_dirs[@]}"} ) + done fi if [ "${#libs[@]}" -gt 0 ]; then @@ -157,6 +161,8 @@ runc() ( runcxx() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + local -a runc_flags=(${runcxx_flags[@]+"${runcxx_flags[@]}"}) BASH_ENV=<( declare -p runc_flags ) \ runc_compiler="${runcxx_compiler:-g++}" \ @@ -165,6 +171,7 @@ runcxx() ( _runc_gdb() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} BINARY CORE_DUMP" >&2 @@ -179,6 +186,7 @@ _runc_gdb() ( _runc_lldb() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} BINARY CORE_DUMP" >&2 @@ -193,6 +201,7 @@ _runc_lldb() ( runc_debug() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} BINARY" >&2 diff --git a/%HOME%/.bash_utils/distr.sh b/%HOME%/.bash_utils/distr.sh index 232bbee..6d382e8 100644 --- a/%HOME%/.bash_utils/distr.sh +++ b/%HOME%/.bash_utils/distr.sh @@ -10,6 +10,7 @@ sums_name="$( basename -- "$sums_path" )" _sums_unescape_path() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} PATH" >&2 @@ -24,6 +25,7 @@ _sums_unescape_path() ( sums_list_paths() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local print_lines= local print_sums= @@ -87,20 +89,22 @@ sums_list_paths() ( } <<< "$line" done < "$sums_path" - [ "${#output[@]}" -gt 0 ] && printf -- "$fmt_output" ${output[@]+"${output[@]}"} + if [ "${#output[@]}" -gt 0 ]; then + printf -- "$fmt_output" ${output[@]+"${output[@]}"} + fi ) sums_add() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -A existing local -a missing=() local path - - while IFS= read -d '' -r path; do + sums_list_paths -z | while IFS= read -d '' -r path; do existing[$path]=1 - done < <( sums_list_paths -z ) + done for path; do [ -z "${existing[$path]+x}" ] && missing+=("$path") @@ -113,47 +117,52 @@ sums_add() ( sums_add_all() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -a paths - local path - while IFS= read -d '' -r path; do + local path + find . -type f -\! -iname "$sums_name" -printf '%P\0' | while IFS= read -d '' -r path; do paths+=("$path") - done < <( find . -type f -\! -iname "$sums_name" -printf '%P\0' ) + done sums_add ${paths[@]+"${paths[@]}"} ) sums_add_distr() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -a paths local path + find . -type f -\( \ + -iname '*.7z' \ + -o -iname '*.exe' \ + -o -iname '*.img' \ + -o -iname '*.iso' \ + -o -iname '*.tar' \ + -o -iname '*.tar.bz2' \ + -o -iname '*.tar.gz' \ + -o -iname '*.zip' \ + -\) -printf '%P\0' | while IFS= read -d '' -r path; do paths+=("$path") - done < <( find . -type f -\( \ - -iname '*.7z' \ - -o -iname '*.exe' \ - -o -iname '*.img' \ - -o -iname '*.iso' \ - -o -iname '*.tar' \ - -o -iname '*.tar.bz2' \ - -o -iname '*.tar.gz' \ - -o -iname '*.zip' \ - -\) -printf '%P\0' ) + done sums_add ${paths[@]+"${paths[@]}"} ) sums_verify() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit sha1sum --check --strict --quiet -- "$sums_path" ) sums_remove_missing() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local dry_run= @@ -177,13 +186,13 @@ sums_remove_missing() ( local -a input=() local -a paths=() - local line path - while IFS= read -d '' -r line; do + local line path + sums_list_paths -z --lines | while IFS= read -d '' -r line; do IFS= read -d '' -r path input+=("$line") paths+=("$path") - done < <( sums_list_paths -z --lines ) + done local -a output=() diff --git a/%HOME%/.bash_utils/file.sh b/%HOME%/.bash_utils/file.sh index 684ff4b..ba58031 100644 --- a/%HOME%/.bash_utils/file.sh +++ b/%HOME%/.bash_utils/file.sh @@ -7,6 +7,7 @@ swap_files() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} PATH1 PATH2" >&2 diff --git a/%HOME%/.bash_utils/git.sh b/%HOME%/.bash_utils/git.sh index e366f07..021e0e8 100644 --- a/%HOME%/.bash_utils/git.sh +++ b/%HOME%/.bash_utils/git.sh @@ -12,9 +12,15 @@ alias repo_branches='git for-each-ref '"'"'--format=%(refname:short)'"'"' refs/h workdir_is_clean() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + local status status="$( git status --porcelain )" - test -z "$status" + + if [ -n "$status" ]; then + echo "${FUNCNAME[0]}: repository isn't clean" >&2 + return 1 + fi ) alias workdir_clean_all='git clean -fdx' @@ -22,16 +28,14 @@ alias workdir_clean_ignored='git clean -fdX' branch_eol_normalized() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe - if ! workdir_is_clean; then - echo "${FUNCNAME[0]}: repository isn't clean" >&2 - return 1 - fi + workdir_is_clean local normalized=0 local line - while IFS= read -d '' -r line; do + git ls-files -z --eol | while IFS= read -d '' -r line; do local eolinfo if ! eolinfo="$( expr "$line" : 'i/\([^ ]*\)' )"; then echo "${FUNCNAME[0]}: couldn't extract eolinfo from: $line" >&2 @@ -53,28 +57,27 @@ branch_eol_normalized() ( fi normalized=1 - done < <( git ls-files -z --eol ) + done return "$normalized" ) repo_eol_normalized() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe - if ! workdir_is_clean; then - echo "${FUNCNAME[0]}: repository isn't clean" >&2 - return 1 - fi + workdir_is_clean local branch - while IFS= read -r branch; do + repo_branches | while IFS= read -r branch; do git checkout --quiet "$branch" branch_eol_normalized "$branch" - done < <( repo_branches ) + done ) workdir_tighten_permissions() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit branch_files -z | xargs -0 -- chmod 0600 -- branch_dirs -z | xargs -0 -- chmod 0700 -- @@ -83,32 +86,35 @@ workdir_tighten_permissions() ( branch_doslint() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -a paths - local path - while IFS= read -d '' -r path; do + local path + branch_files -z | while IFS= read -d '' -r path; do paths+=("$path") - done < <( branch_files -z ) + done doslint ${paths[@]+"${paths[@]}"} ) branch_lint() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local -a paths - local path - while IFS= read -d '' -r path; do + local path + branch_files -z | while IFS= read -d '' -r path; do paths+=("$path") - done < <( branch_files -z ) + done lint ${paths[@]+"${paths[@]}"} ) branch_backup() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local repo_dir repo_dir="$( pwd )" @@ -135,6 +141,8 @@ branch_backup() ( branch_backup_dropbox() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + branch_backup "$USERPROFILE/Dropbox/backups" ) diff --git a/%HOME%/.bash_utils/os.sh b/%HOME%/.bash_utils/os.sh index 1e66108..bda1c06 100644 --- a/%HOME%/.bash_utils/os.sh +++ b/%HOME%/.bash_utils/os.sh @@ -53,6 +53,7 @@ os_is_arch_based() { os_is_arch || os_is_manjaro ; } pkg_list_cygwin() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit cygcheck --check-setup --dump-only \ | tail -n +3 \ @@ -63,6 +64,7 @@ pkg_list_cygwin() ( setup_pkg_list_ubuntu() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local -r initial_status='/var/log/installer/initial-status.gz' @@ -74,13 +76,20 @@ setup_pkg_list_ubuntu() ( user_pkg_list_ubuntu() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit - comm -23 <( apt-mark showmanual | sort | uniq ) \ - <( setup_pkg_list_ubuntu ) + local manual + manual="$( apt-mark showmanual | sort | uniq )" + + local setup + setup="$( setup_pkg_list_ubuntu )" + + comm -23 <( echo "$manual" ) <( echo "$setup" ) ) pkg_list_ubuntu() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit dpkg --get-selections \ | grep --invert-match -- 'deinstall$' \ @@ -92,6 +101,7 @@ pkg_list_ubuntu() ( setup_pkg_list_arch() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit # Assuming you only selected groups 'base and 'base-devel' during # installation. @@ -102,21 +112,35 @@ setup_pkg_list_arch() ( user_pkg_list_arch() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit + + local manual + manual="$( pacman -Q --explicit -q | sort )" + + local setup + setup="$( setup_pkg_list_arch )" - comm -23 <( pacman -Q --explicit -q | sort ) \ - <( setup_pkg_list_arch ) + comm -23 <( echo "$manual" ) <( echo "$setup" ) ) user_pkg_list_manjaro() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit # https://forum.manjaro.org/t/how-do-i-get-a-list-of-all-packages-installed-on-a-system/50289/8 - comm -12 <( grep --perl-regexp --only-matching -e '\[ALPM\] installed \K\S*' /var/log/pacman.log | sort | uniq ) \ - <( pacman -Qeqn | sort ) + + local setup + setup="$( grep --perl-regexp --only-matching -e '\[ALPM\] installed \K\S*' /var/log/pacman.log | sort | uniq )" + + local manual + manual="$( pacman -Qeqn | sort )" + + comm -12 <( echo "$setup" ) <( echo "$manual" ) ) manual_pkg_list_arch() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit expac '%n %p' | grep 'Unknown Packager' ) @@ -137,6 +161,7 @@ pkg_list_manjaro() { pkg_list_fedora() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit rpm --queryformat="%{NAME}\n" -qa | sort ) @@ -145,6 +170,7 @@ pkg_list_fedora() ( pkg_list() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if os_is_cygwin; then pkg_list_cygwin @@ -164,6 +190,7 @@ pkg_list() ( user_pkg_list() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if os_is_ubuntu || os_is_mint; then user_pkg_list_ubuntu diff --git a/%HOME%/.bash_utils/path.sh b/%HOME%/.bash_utils/path.sh index c8c4613..326cbf7 100644 --- a/%HOME%/.bash_utils/path.sh +++ b/%HOME%/.bash_utils/path.sh @@ -11,15 +11,16 @@ path_add() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe [ "$#" -eq 0 ] && return 0 local -a src_list - local path - while IFS= read -d '' -r path; do + local path + readlink -z --canonicalize-missing -- "$@" | while IFS= read -d '' -r path; do src_list+=("$path") - done < <( readlink -z --canonicalize-missing -- "$@" ) + done for path; do if str_contains "$path" ':'; then @@ -38,11 +39,11 @@ path_add() ( done if [ -n "${PATH-}" ]; then - while IFS= read -d '' -r path; do + str_split -z -- "${PATH-}" ':' | xargs -0 -- readlink -z --canonicalize-missing -- | while IFS= read -d '' -r path; do [ -n "${dest_dict[$path]+x}" ] && continue dest_dict[$path]=1 dest_list+=("$path") - done < <( str_split -z -- "${PATH-}" ':' | xargs -0 -- readlink -z --canonicalize-missing -- ) + done fi str_join ':' ${dest_list[@]+"${dest_list[@]}"} @@ -50,7 +51,12 @@ path_add() ( path_export() { local new_path + local ret + + new_path="$( path_add "$@" )" + ret="$?" + + [ "$ret" -ne 0 ] && return "$ret" - new_path="$( path_add "$@" )" \ - && export PATH="$new_path" + export PATH="$new_path" } diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh index 0ea007f..e4e5929 100644 --- a/%HOME%/.bash_utils/text.sh +++ b/%HOME%/.bash_utils/text.sh @@ -28,6 +28,7 @@ doslint() { _sed_escape_pattern() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} STR" >&2 @@ -51,6 +52,7 @@ _sed_escape_pattern() ( _sed_escape_substitution() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} STR" >&2 @@ -67,6 +69,7 @@ _sed_escape_substitution() ( file_replace() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -lt 3 ]; then echo "usage: ${FUNCNAME[0]} OLD NEW PATH..." >&2 @@ -85,6 +88,7 @@ file_replace() ( file_replace_word() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -lt 3 ]; then echo "usage: ${FUNCNAME[0]} OLD NEW PATH..." >&2 @@ -103,6 +107,7 @@ file_replace_word() ( str_replace() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 3 ]; then echo "usage: ${FUNCNAME[0]} STR SUB REP" >&2 @@ -120,6 +125,7 @@ str_replace() ( str_tolower() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local s for s; do @@ -129,6 +135,7 @@ str_tolower() ( str_toupper() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit local s for s; do @@ -138,6 +145,7 @@ str_toupper() ( _bash_escape_pattern() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} STR" >&2 @@ -157,6 +165,7 @@ _bash_escape_pattern() ( str_contains() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} STR SUB" >&2 @@ -174,6 +183,7 @@ str_contains() ( str_starts_with() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} STR SUB" >&2 @@ -191,6 +201,7 @@ str_starts_with() ( str_ends_with() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 2 ]; then echo "usage: ${FUNCNAME[0]} STR SUB" >&2 @@ -208,6 +219,7 @@ str_ends_with() ( str_split() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit lastpipe local fmt='%s\n' local -a args @@ -255,13 +267,14 @@ str_split() ( # Thanks to this guy for this trick: # http://stackoverflow.com/a/24426608/514684 - IFS="$delim" read -a xs -d '' -r < <( printf -- "%s$delim\\0" "$str" ) + printf -- "%s$delim\\0" "$str" | IFS="$delim" read -a xs -d '' -r [ "${#xs[@]}" -gt 0 ] && printf -- "$fmt" ${xs[@]+"${xs[@]}"} ) str_join() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -lt 1 ]; then echo "usage: ${FUNCNAME[0]} DELIM [STR]..." >&2 @@ -285,6 +298,7 @@ str_join() ( str_grep() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} PATTERN" >&2 @@ -298,6 +312,7 @@ str_grep() ( str_grep_word() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} PATTERN" >&2 @@ -311,6 +326,7 @@ str_grep_word() ( str_iconv_fromto() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -lt 3 ]; then echo "usage: ${FUNCNAME[0]} SRC_ENCODING DEST_ENCODING PATH..." >&2 @@ -338,6 +354,7 @@ str_iconv_fromto() ( str_iconv() ( set -o errexit -o nounset -o pipefail + shopt -s inherit_errexit if [ "$#" -lt 2 ]; then echo "usage: ${FUNCNAME[0]} DEST_ENCODING PATH..." >&2 |