views:

161

answers:

1

I am creating a script to configure and launch software. My current application requires appending to LD_LIBRARY_PATH. I know that I can test if this is empty first and set it outright and append it otherwise.

The following does not appear to work and it keeps appending to the variable even if it contains the string I am testing for in the case statement.

mysetup.sh

export AXIS2C_HOME=/home/ojblass/Source/axis2c-src-1.6.0
if [ "$LD_LIBRARY_PATH" == "" ]; then
   echo empty setting
   export LD_LIBRARY_PATH=${AXIS2C_HOME}/lib
else
   case $LD_LIB_PATH in
   *$AXIS2C_HOME/lib* )
       echo already there skipping;;
   * )
       echo has entries but appending
       export LD_LIBRARY_PATH=${AXIS2C_HOME}/lib:${LD_LIBRARY_PATH};;
   esac
fi
echo AXIS2C_HOME=${AXIS2C_HOME}
echo LD_LIBRARY_PATH=${LD_LIBRARY_PATH}

Example_Run

ojblass@helium:~/Source/axis2c-src-1.6.0$ pwd
/home/ojblass/Source/axis2c-src-1.6.0
ojblass@helium:~/Source/axis2c-src-1.6.0$ . ./mysetup.sh
empty setting
AXIS2C_HOME=/home/ojblass/Source/axis2c-src-1.6.0
LD_LIBRARY_PATH=/home/ojblass/Source/axis2c-src-1.6.0/lib
ojblass@helium:~/Source/axis2c-src-1.6.0$ . ./mysetup.sh
has entries but appending
AXIS2C_HOME=/home/ojblass/Source/axis2c-src-1.6.0
LD_LIBRARY_PATH=/home/ojblass/Source/axis2c-src-1.6.0/lib:/home/ojblass/Source/axis2c-src-1.6.0/lib

As you notice the LD_LIBRARY_PATH keeps getting added to. Also I am a bit fearful of being shell specific and not being as portable as I would like. Any advice is appreciated.

+2  A: 

If the code above is copied and pasted, then the answer is trivial.

Spell LD_LIBRARY_PATH Consistently

(The occurrence of LD_LIB_PATH is probably not what you had in mind.)

When tested using:

sh -x mysetup.sh
(. ./mysetup.sh; sh -x mysetup.sh)
(unset LD_LIBRARY_PATH; sh -x mysetup.sh)

I saw that $AXIS2C_HOME/lib was added by the first command, and that it was not added twice by the second command sequence, and that the third command sequence just set it.

An alternative mechanism is to use a script such as clnpath documented in How to keep from duplicating path variable in csh, or one of the other answers there. Basically, they analyze a path-like variable and ensure it contains no duplicates.

Jonathan Leffler
DOH! It is painful to work on HP, AIX, NCR, and Linux all in one day. Thanks Jonathan!
ojblass