From 42da15c1fbff60c4a49705b96ebb30721ccafb14 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 2 Apr 2022 08:18:01 +0300 Subject: bash: best practices & linting --- %HOME%/.bash_utils/path.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to '%HOME%/.bash_utils/path.sh') 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" } -- cgit v1.2.3