Walter
Walter

Reputation: 45444

how to remove a path from LD_LIBRARY_PATH in tcsh?

on exiting/unloading a package, I must reset the LD_LIBRARY_PATH back to its previous setting by removing a certain path from it. How to do that in tcsh or csh?

edit1 to clarify.

Say the LD_LIBRARY_PATH is something like path1:mypath:path2:path3:path4 and I do not know (in my script) anything about it except that it must contain mypath, [begin edit2 which is stored in the variable $MY_PATH end edit2] somewhere. Then I want to remove mypath, such that after the operation the LD_LIBRARY_PATH is path1:path2:path3:path4.

Upvotes: 4

Views: 7306

Answers (2)

Kenigmatic
Kenigmatic

Reputation: 436

I would have added this as a comment to robert's answer, but I do not have 50 reputation. Yet. (Hint: You can help fix that! ;-)

I'm a csh user (not by choice), so robert's answer is greatly appreciated. But the regex used is susceptible to matching partial path elements, e.g., if LD_LIBRARY_PATH = path1:mypath:path2:path3:path4, then ...

setenv LD_LIBRARY_PATH `echo $LD_LIBRARY_PATH | perl -pe "s[/ath][]g;"`

... would yield LD_LIBRARY_PATH = p1:my:p2:p3:p4

Building on robert's answer, I've modified the regex to ensure matching only to a complete element in the path string, and put in multiple substitutions to handle the case of DIR_TO_REMOVE occurring at the beginning, middle, or end of the path. I also added a substitutions to clean up leading colon (or path is only ":") and double colons ("::"). I couldn't figure out how to safely remove trailing colons since the csh parser is a pain about handling $ as a meta-character for EOF (or end-of-string, in this case). (see http://www.grymoire.com/Unix/CshTop10.txt)

I've implemented this as a csh alias that accepts command-line arguments, since I wanted to make use of it with PATH, LD_LIBRARY_PATH, et al. The first parameter is the name of the path-variable to be modified. The second parameter is the path-element to replace and the third parameter is the new path-element (or "" to simply remove the path-element matching the 2nd parameter).

I know Walter's question was how to remove a path, but since removing is really just replacing with a null value, This does still answer the question. For my own use, I have different tool chains installed (e.g., GCC 4.1.2, GCC 4.8.1, ICPC 2013) and needed to easily swap-in/swap-out paths to the appropriate binaries, libraries and includes for the given tool chain, but keeping it transparent to the build management system.

Here's the code:

alias repath 'setenv \!:1 `echo $\!:1\: | perl -pe "s[^\!:2\:][\!:3\:]g; s[\:\!:2\:][\:\!:3\:]g; s[\:\:][\:]g; s[^\:][]g;"` '

Safely test it out in a csh terminal session as follows:

setenv myTestPath /myLDpath
setenv myTestPath2 /myLDpath/x86_64
setenv myTestPath3 /myLDpathDebug

setenv testPATH /SOME/PATH:/SOME/OTHER/PATH:${myTestPath}:${myTestPath2}:${myTestPath3}
echo "        Starting with the following path-variable content ..."
printenv testPATH
echo "        Remove $myTestPath from the middle ..."
repath testPATH $myTestPath ""
printenv testPATH
echo "        Put $myTestPath at the beginning ..."
setenv testPATH ${myTestPath}:/SOME/PATH:/SOME/OTHER/PATH:${myTestPath2}:${myTestPath3}
printenv testPATH
echo "        and remove it from the beginning ..."
repath testPATH $myTestPath ""
printenv testPATH
echo "        Put $myTestPath at the end ..."
setenv testPATH /SOME/PATH:/SOME/OTHER/PATH:${myTestPath3}:${myTestPath2}:${myTestPath}
printenv testPATH
echo "        and remove it from the end ..."
repath testPATH $myTestPath ""
printenv testPATH
echo "        Replace $myTestPath3 with /myLDpath/Debug ..."
repath testPATH $myTestPath3 "/myLDpath/Debug"
printenv testPATH
echo "        Done!"

Upvotes: 2

robert
robert

Reputation: 34408

I'm a bash user, but I'm pretty sure this will do the job for you. It will most often leave a :: in $LD_LIBRARY_PATH, but that shouldn't cause any problems.

setenv LD_LIBRARY_PATH `echo $LD_LIBRARY_PATH | perl -pe "s[$DIR_TO_REMOVE][]g;"`

For bash users who might come upon this, use the following command:

LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | perl -pe "s[$DIR_TO_REMOVE][]g;")
export LD_LIBRARY_PATH

Upvotes: 5

Related Questions