bash 在 shell 脚本中执行命令时设置 LD_PRELOAD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16755662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set LD_PRELOAD when executing a command in shell script
提问by Coaku
I want to execute a command like this: "LD_PRELOAD=/path/to/my/so ./a.out"
我想执行这样的命令:“ LD_PRELOAD=/path/to/my/so ./a.out”
so I wrote a shell script:
所以我写了一个shell脚本:
cmd="LD_PRELOAD=/path/to/my/so ./a.out"
${cmd}
Error occured:
发生了错误:
LD_PRELOAD=/path/to/my/so : no such file or directory
By the way, the file /path/to/my/soexists and I can successfully execute the command in a bash.
顺便说一句,该文件/path/to/my/so存在,我可以在 bash 中成功执行该命令。
Anything wrong?
哪里不对了?
回答by Ernest Friedman-Hill
It would be more traditional to just do something like this in your script:
在你的脚本中做这样的事情会更传统:
export LD_PRELOAD=whatever
./a.out
回答by Cairnarvon
It's looking for an executable called LD_PRELOAD=/path/to/my/soin your path and can't find it. You can use evalto get around this:
它正在寻找LD_PRELOAD=/path/to/my/so在您的路径中调用的可执行文件,但找不到它。你可以eval用来解决这个问题:
eval $CMD
Or, equivalently:
或者,等效地:
bash -c "$CMD"
回答by n. 'pronouns' m.
Yes.
是的。
There are several stages in bash command processing. At the very first stage variable assignment and redirections are identified and set aside for further processing. The rest of the command is then passed to the second stage. Variable expansion, along with other things, occurs at that stage.
bash 命令处理有几个阶段。在第一阶段,变量分配和重定向被识别并留作进一步处理。命令的其余部分然后被传递到第二阶段。变量扩展以及其他事情发生在那个阶段。
Words like FOO=barthat result from the second stage are no longer interpreted as variable assignments.
类似FOO=bar第二阶段结果的词不再被解释为变量赋值。
If you want to compress a command with variable assignments and/or redirections into a single word, I recommend writing a function.
如果您想将带有变量赋值和/或重定向的命令压缩为一个单词,我建议您编写一个函数。

