kjh
kjh

Reputation: 3411

LD_LIBRARY_PATH fails in bash script

I have a bash script that runs this line of code:

LD_LIBRARY_PATH=/tools/cluster/6.2/openbabel/2.3.2/lib ./xattr infile.txt outfile.txt

If I were to call this line directly from the shell, it works fine. However if I run it in the bash script I get this error:

update.sh: line 45: LD_LIBRARY_PATH=/tools/cluster/6.2/openbabel/2.3.2/lib: No such file or directory

Why doesn't LD_LIBRARY_PATH work when it's set in a bash script?

Here's more of the code around line 45:

BASE_DIR="/volatile/huanlab/bold/kendal/bioinformatics_database/tmp"
COMP_DIR="$BASE_DIR/compound"

# move to the current directory where xattr.cpp and other files are
cd /users/kharland/software/programs/BioDB-update/dev

# Compile xattr ('make xattr' is the same command I call from the shell
# to compile this program when this program actually works).
make xattr

# loop over each .sdf file in COMP_DIR
for INF in $(ls $COMP_DIR | grep sdf)
do
    babel -isdf $COMP_DIR/$INF -ocan $SMILES_DIR/$INF.csv
    LD_LIBRARY_PATH=/tools/cluster/6.2/openbabel/2.3.2/lib ./xattr $COMP_DIR/$INF $COMP_DIR/$COMP_FILE
done

The contents before these lines are just comments

edit

In My makefile, I am compiling with these options

LDLIBS=-lm -ldl -lz -lopenbabel
LDFLAGS=-Wl,-rpath,/tools/cluster/6.2/openbabel/2.3.2/lib:/tools/cluster/system/pkg/openbabel/openbabel-2.3.2/build/lib,-L/tools/cluster/6.2/openbabel/2.3.2/lib

and running ldd xattr shows that the libraries are indeed linked, so the program executes as expected when invoked from the shell. The only issue is with the bash script. If I remove the LD_LIBRARY_PATH option from the bash script I get an issue where the shared libraries for openbabel aren't found even though ldd shows that xattr knows where the libs are. That's why I have LD_LIBRARY_PATH added in the bash script, I'm attempting to use it as a workaround

edit
(corrected mistake: swapped 'libraries' with 'my code' below) (had wrong file system name below)

Something just occurred to me. My source code is in the /users file system. If my libraries are on a different, mounted file system, would bash have trouble finding these documents?

Upvotes: 2

Views: 3325

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295815

Setting environment variables does work in bash scripts.

Try this:

#!/bin/bash
VAR1=VALUE1 env

...run that script, and you'll see output that includes VAR1 and its value.

Generally speaking, this also works with LD_LIBRARY_PATH:

#!/bin/bash
tempdir=$(mktemp -t -d testdir.XXXXXX)
LD_LIBRARY_PATH=$tempdir env
rm -rf "$tempdir"

If you can generate a minimal reproducer in which this doesn't occur, that would be helpful and appreciated.

Upvotes: 2

Related Questions