X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=.profile.d%2FPATH.bashrc;h=b27435ae9227fb6efb60f04af8a2727a8fbff94d;hb=f5757053144627072421376776ac959f70af72d0;hp=0b82245160706099a2ecb673f6e11bd5d550b489;hpb=f00d519dd073659e3a079be65c57d5b64cb60a69;p=profile.git diff --git a/.profile.d/PATH.bashrc b/.profile.d/PATH.bashrc index 0b82245..b27435a 100644 --- a/.profile.d/PATH.bashrc +++ b/.profile.d/PATH.bashrc @@ -14,7 +14,6 @@ PATH:bindirs C_INCLUDE_PATH:incdirs CPLUS_INCLUDE_PATH:@C_INCLUDE_PATH LD_LIBRARY_PATH:libdirs -LD_RUN_PATH:@PATH MANPATH:mandirs PKG_CONFIG_PATH:pkgdirs " @@ -49,16 +48,35 @@ function copypath() { [ -z "$path" ] || eval "export $newpath='$path'" } +# Try to expand variables in a path definition, discarding it if expansion fails. +function expandpath() { + local path="$1"; shift + + while [ -n "$path" ]; do + [ "${path/\$/}" = "$path" ] && return 0 + path="${path#*\$}" + + local var="${path%%/*}" + eval "[ -n \"\$$var\" ] || return 1" + done + + return 0 +} + # Helper. function addpath() { local path="$1"; shift local dir="$1"; shift - dir=$(eval echo "$dir") - if [ -d "$dir" ]; then - echo "$path:$dir" - else + if ! expandpath "$dir"; then echo "$path" + else + dir=$(eval echo "$dir") + if [ -d "$dir" ]; then + echo "$path:$dir" + else + echo "$path" + fi fi } @@ -129,4 +147,4 @@ for path in $PATHS; do done -unset DIR PATHS dir dirs path var source sanitisepath copypath makepath newpath addpath +unset DIR PATHS dir dirs path var source expandpath sanitisepath copypath makepath newpath addpath