I have a file which is sourced by an application, adding relevant directories to $PATH and $LD_LIBRARY_PATH.
This is fine, but it results in somewhat excessive pollution of the $PATH and $LD_LIBRARY_PATH, since quite a few paths are added, about 2/3 of which don't exist.
I can test for the existence of the directory using [ ! -d $VARIABLE_TO_ADD_TO_PATH ]
, but if I find the directory doesn't exist, unsetting it seems to be harder.
I can write (for each of maybe 10 variables)
if [ ! -d $VAR ]
unset VAR
fi
but I'd rather have a function.
I tried:
_testPathAndUnset()
{
while [ $# -ge 1 ]
do
dirToTest=${1/startPattern/\$startPattern}
if [ ! -d $dirToTest ]
unset $1
fi
shift
done
}
passing in values in a form like _testPathAndUnset VAR1 VAR2 VAR3
(not $VAR1 $VAR2 $VAR3
), but now I have strings which don't expand.
Using eval
might solve this for me, but other questions, answers, websites etc etc suggest this is generally a bad idea.
How can I solve this problem in a better way than 30 lines of 10x3 ?
if [ ! -d $VAR ]
unset VAR
fi