aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-10-27 06:20:40 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-10-27 06:20:40 +0300
commite5b926055b733227c97d0fcc9444a4b20a16d1a7 (patch)
tree7586b4e9d5cc883b2a31381163f92f8f9672f19c
parentadd str_starts_with, str_split, str_join (diff)
downloadlinux-home-e5b926055b733227c97d0fcc9444a4b20a16d1a7.tar.gz
linux-home-e5b926055b733227c97d0fcc9444a4b20a16d1a7.zip
add path.sh
-rw-r--r--%HOME%/.bash_utils/path.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/%HOME%/.bash_utils/path.sh b/%HOME%/.bash_utils/path.sh
new file mode 100644
index 0000000..322e802
--- /dev/null
+++ b/%HOME%/.bash_utils/path.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Cygwin configuration files" project.
+# For details, see https://github.com/egor-tensin/cygwin-home.
+# Distributed under the MIT License.
+
+source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/text.sh"
+
+add_missing_path() (
+ set -o errexit -o nounset -o pipefail
+
+ [ "$#" -eq 0 ] && return 0
+
+ local -a new_paths
+ local path
+
+ while IFS= read -d '' -r path; do
+ new_paths+=("$path")
+ done < <( readlink --zero --canonicalize-missing "$@" )
+
+ for path; do
+ if str_contains "$path" ':'; then
+ echo "${FUNCNAME[0]}: mustn't contain colons: $path" >&2
+ return 1
+ fi
+ done
+
+ local -A old_paths
+
+ while IFS= read -d '' -r path; do
+ old_paths[$path]=1
+ done < <( str_split -z -- "${PATH-}" ':' | xargs --null readlink --zero --canonicalize-missing )
+
+ for path in ${new_paths[@]+"${new_paths[@]}"}; do
+ old_paths[$path]=1
+ done
+
+ str_join ':' "${!old_paths[@]}"
+)
+
+add_path() {
+ PATH="$( add_missing_path "$@" )" || return $?
+ export PATH
+}