linux - LD_LIBRARY_PATH 在 bash 脚本中失败

标签 linux bash shell ld

我有一个运行这行代码的 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/

相关文章:

linux - 用个人访问 token 推送到git

Bash Shell 参数扩展 ${ }

r - 如何在 Windows 上从源 tarball 安装 R 包?

python - 如何在反向 shell 中处理 rm 和 cp 命令

linux - Chef-client 是否在启动时运行?

c++ - atexit() 函数

linux - 如何使用 <ctrl>c 从 script-1.sh 杀死 script-0.sh?

c++ - 如何使用 Genlex 构建 OCaml 源代码

linux - 根据文本文件中的部分名称将文件复制或移动到另一个目录

Linux 使用 "ln"命令链接