# -*- mode: sh; -*- # # $HeadURL: https://tstotts.net/pubvc/bash-snippets/paths.sh $ # $Id: paths.sh 435 2007-01-18 20:32:26Z tas $ # # # Test that a value is in a path variable proper. # $1 - PATH variable name # $2 - value to test # Example usage: # f_path_test PATH /bin # f_path_test MANPATH /usr/local/man # f_path_test() { [[ x${!1} != x${!1%${2}:*} ]] && return 0; [[ x${!1} != x${!1#*:${2}} ]] && return 0; [[ ${!1} = x$2 ]] && return 0; return 1 } # # Append one or more values to a path variable proper, iff that path # does not yet contain the value (avoiding duplicates). Does not # require value to be a valid filesystem path name. # $1 - PATH variable name # $2.. - values # f_path_append() { n=$1; shift; for p in "$@"; do f_path_test $n "${p}" || export ${n}="${!n}:${p}" done } f_path_prepend() { n=$1; shift; for p in "$@"; do f_path_test $n "${p}" || export ${n}="${p}:${!n}" done } # # Append one or more values to a path variable proper, iff that path # does not yet contain the value (avoiding duplicates) and the value # is a valid filesystem path name. # $1 - PATH variable name # $2.. - values # f_path_append_name() { n=$1; shift; for p in "$@"; do if [[ -e "${p}" ]]; then f_path_test $n "${p}" || export ${n}="${!n}:${p}" fi done } f_path_prepend_name() { n=$1; shift; for p in "$@"; do if [[ -e "${p}" ]]; then f_path_test $n "${p}" || export ${n}="${p}:${!n}" fi done } # add some user paths should they exist ps=(/bin /usr/bin /usr/local/bin /opt/bin /opt/local/bin) f_path_append_name PATH "${ps[@]}" ps=($HOME/bin) f_path_prepend_name PATH "${ps[@]}" # add some admin paths should they exist ps=(/sbin /usr/sbin /usr/local/sbin /opt/sbin /opt/local/sbin) f_path_append_name PATH "${ps[@]}" # Only if these path variable already exist, ensure certain paths for # searching man and info pages. ms=(/usr/man /usr/share/man /usr/local/man /usr/local/share/man) [[ -n "$MANPATH" ]] && f_path_append_name MANPATH "${ms[@]}" is=(/usr/info /usr/share/info /usr/local/info /usr/local/share/info) [[ -n "$INFOPATH" ]] && f_path_append_name INFOPATH "${is[@]}" is=($HOME/.info) [[ -n "$INFOPATH" ]] && f_path_prepend_name INFOPATH "${is[@]}" # add some frequent directories to CDPATH for easy cd'ing export CDPATH='.' cs=(~ ~/svn_work/) f_path_append_name CDPATH "${cs[@]}"