# Created: Sat Feb 24 00:17:49 EST 2001 # Name: path-funcs-sh, path-funcs # Copyright 2001-2007, Chris F.A. Johnson # Released under the terms of the GNU General Public License ## Functions to manipulate the PATH variable path() ## add arguments to PATH; if no arguments, show PATH { if [ -n "$*" ] then addpath "$@" else IFS=: eval "printf \"%s\n\" \$PATH" fi } addpath() { # add directory or directories to $PATH ap_prefix=0 ap_quiet=0 OPTIND=1 while getopts iq var do case "$var" in i) ap_prefix=1 ;; q) ap_quiet=1 ;; esac done shift $(( $OPTIND - 1 )) for p in "$@" do p=${p%"${p##*[!/]}"} ## remove trailing slashes case $p in ""|.) continue ;; esac case :$PATH: in *:$p:*) [ $ap_quiet -eq 0 ] && echo "addpath: $p already in path" >&2 continue ;; esac if [ -d "$p" ] then if [ $ap_prefix -eq 1 ] then PATH="$p:$PATH" else PATH="$PATH:$p" fi else [ $ap_quiet -eq 0 ] && echo "addpath: $p is not a directory" >&2 fi done export PATH } checkpath() # verify that all entries in $PATH are directories; remove dupes { verbose=0 OPTIND=1 while getopts v var do case "$var" in v) verbose=1 ;; esac done ## assign the elements in PATH to the positional parameters oldIFS=$IFS IFS=":" set -- $PATH IFS=$oldIFS newPATH= for p do case $p in ""|.) continue ;; ## do not allow current directory esac if [ -d "$p" ] then p=${p%"${p##*[!/]}"} ## remove trailing slashes case :$newPATH: in *:"$p":*) [ $verbose -ge 1 ] && echo "checkpath: removing $p (already in PATH)" >&2 ;; *) newPATH=${newPATH:+$newPATH:}$p ;; esac else [ $verbose -ge 1 ] && echo "checkpath: $p is not a directory; removing it from PATH" >&2 fi done export PATH=$newPATH return } rmpath() # remove directory or directories from $PATH { for p in "$@" do p=${p%"${p##*[!/]}"} ## remove trailing slashes case $PATH in "$p":*) PATH=${PATH#$p:} ;; ## at beginning of PATH *:"$p") PATH=${PATH%:$p} ;; ## at end of PATH *:"$p":*) PATH=${PATH%":$p"*}${PATH##*"$p"} ;; esac done export PATH }