我有一个运行这行代码的 bash 脚本:
LD_LIBRARY_PATH=/tools/cluster/6.2/openbabel/2.3.2/lib ./xattr infile.txt outfile.txt
如果我直接从 shell 调用此行,它可以正常工作。但是,如果我在 bash 脚本中运行它,我会收到此错误:
update.sh: line 45: LD_LIBRARY_PATH=/tools/cluster/6.2/openbabel/2.3.2/lib: No such file or directory
为什么在 bash 脚本中设置 LD_LIBRARY_PATH 时不起作用?
以下是第 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
这些行之前的内容只是注释
编辑
在我的 makefile 中,我使用这些选项进行编译
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
并且运行ldd xattr
表明这些库确实被链接,因此当从shell调用时程序按预期执行。唯一的问题是 bash 脚本。如果我从 bash 脚本中删除 LD_LIBRARY_PATH 选项,我会遇到一个问题,即找不到 openbabel 的共享库,即使 ldd 显示 xattr 知道库在哪里。这就是为什么我在 bash 脚本中添加了 LD_LIBRARY_PATH,我尝试将其用作解决方法
编辑
(更正错误:将下面的“库”与“我的代码”交换)
(下面的文件系统名称错误)
我突然想到一件事。我的源代码位于 /users
文件系统中。如果我的库位于不同的已安装文件系统上,bash 会很难找到这些文档吗?
最佳答案
设置环境变量确实在 bash 脚本中起作用。
试试这个:
#!/bin/bash
VAR1=VALUE1 env
...运行该脚本,您将看到包含 VAR1
及其值的输出。
一般来说,这也适用于LD_LIBRARY_PATH
:
#!/bin/bash
tempdir=$(mktemp -t -d testdir.XXXXXX)
LD_LIBRARY_PATH=$tempdir env
rm -rf "$tempdir"
如果您可以生成一个不会发生这种情况的最小再现器,那将很有帮助并值得赞赏。
关于linux - LD_LIBRARY_PATH 在 bash 脚本中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24470946/