##################################################################### # Utilities for adding and subtracting paths from PATH, MANPATH etc # ##################################################################### # Usage: # -p == priority == append to the front of the path. # -r == realpath, e.g. "pathadd ." adds $PWD to the path # -n == modify instead of PATH # # Note: pathadd dedupes new paths against existing ones. # Duplicate entries in the path just slow down lookups. function pathadd { [ -n "$(which sed)" ] || \ { echo "ERROR: Pathadd needs sed. Please install sed. Quitting." >&2 return 1 } local priority=0 local realpath=0 local name=PATH while [ "x-" = "x${1:0:1}" ] do # check this isn't an end of flags - or -- [ "x-" = "x$1" ] && break [ "x--" = "x$1" ] && break # Read the flag argv1="${1:1}" shift 1 while (( ${#argv1} > 0 )) do case "${argv1:0:1}" in n) name="$1" [[ "$name" == +([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_])*([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890]) ]] ||\ { echo "The given \"$name\" is not a valid variable name" return 1 } shift 1 ;; p) priority=1 ;; r) realpath=1 ;; *) echo "Don't recognise flag \"$1\" remainder \"$argv1\"" >&2 return 1 ;; esac argv1="${argv1:1}" done done local path local path_supplement local priority_path_supplement local trailing_path_supplement eval path=\"\$${name}\" while (( $# > 0 )) do if (( realpath == 1 )) then path_supplement="$(realpath $1)" else path_supplement="$1" fi shift 1 if (( priority == 1 )) then priority_path_supplement="$path_supplement" trailing_path_supplement="" else trailing_path_supplement="$path_supplement" priority_path_supplement="" fi path=":${path}:" path="$(echo "$path" | sed "s?:${path_supplement}:?:?g")" path="${priority_path_supplement}:${path}:${trailing_path_supplement}" path="$(echo "$path" | sed 's/^:*//g;s/:*$//g;s/::*/:/g')" done export $name="$path" } function pathsubtract { [ -n "$(which sed)" ] || \ { echo "ERROR: Pathadd needs sed. Please install sed. Quitting." >&2 return 1 } local name=PATH local realpath=0 while [ "x-" = "x${1:0:1}" ] do # check this isn't an end of flags - or -- [ "x-" = "x$1" ] && break [ "x--" = "x$1" ] && break # Read the flag argv1="${1:1}" shift 1 while (( ${#argv1} > 0 )) do case "${argv1:0:1}" in n) name="$1" [[ "$name" == +([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_])*([abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890]) ]] ||\ { echo "The given \"$name\" is not a valid variable name" return 1 } shift 1 ;; r) realpath=1 ;; *) echo "Don't recognise flag \"$1\" remainder \"$argv1\"" >&2 return 1 ;; esac argv1="${argv1:1}" done done local path local path_supplement eval path=\"\$${name}\" while (( $# > 0 )) do if (( realpath == 1 )) then path_supplement="$(realpath $1)" else path_supplement="$1" fi shift 1 path=":${path}:" path="$(echo "$path" | sed "s?:${path_supplement}:?:?g")" path="$(echo "$path" | sed 's/^:*//g;s/:*$//g;s/::*/:/g')" done export $name="$path" } pathrm() { pathsubtract "$@" }