From: Iain Patterson Date: Sat, 2 Mar 2013 00:48:47 +0000 (+0000) Subject: Strip symlinks from PATH. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=3075527fd8e750e8221c89d1ba7e74a952e06067;p=profile.git Strip symlinks from PATH. Remove entries from PATH etc if they are symlinks to other entries which already exist. Useful on Solaris or Fedora 17+ where, for example, /bin is a symlink to /usr/bin. --- diff --git a/.profile.d/PATH.bashrc b/.profile.d/PATH.bashrc index b27435a..38bb825 100644 --- a/.profile.d/PATH.bashrc +++ b/.profile.d/PATH.bashrc @@ -121,6 +121,35 @@ function makepath() { eval "export $newpath='$path'" } +# Remove entries which are symlinks to other existing entries. +function canonicalisepath() { + local path="$1"; shift + local newpath= + + local JGD=$IFS + IFS=' +' + + local dirs=$(eval echo "\$$path") + local check=":$dirs:" + for dir in ${dirs//:/ +}; do + if [ -L "$dir" ]; then + # Is this a symlink to another entry? + local canon=$(readlink -f "$dir" 2>/dev/null) + if [ -n "$canon" ]; then + [ "${check/:$canon:/}" = "$check" ] || continue + fi + fi + + newpath="$newpath:$dir" + done + + IFS=$JGD + + eval "export $path='${newpath##:}'" +} + # Construct directory list, omitting nonexistent and undefined ones. dirs= for dir in "${SYSTEM:-@}/${ARCHITECTURE:-@}" "${SYSTEM:-@}" ""; do @@ -141,10 +170,10 @@ for path in $PATHS; do [ -e "$dir" ] || continue makepath "$var" "$dir" done + canonicalisepath "$var" else copypath "$var" "${source#@}" fi done - -unset DIR PATHS dir dirs path var source expandpath sanitisepath copypath makepath newpath addpath +unset DIR PATHS dir dirs path var source expandpath sanitisepath copypath makepath newpath addpath canonicalisepath