如何将环境设置导入到我的 Perl 程序中?
我有一个脚本,其内容只是导出 Linux 中的一个变量。
export LD_LIBRARY_PATH=....
我想在我的 Perl 脚本中运行此脚本,以便运行我的 Perl 脚本的人都将设置他们的 LD_LIBRARY_PATH 。我可以在 Perl 脚本的开头执行此操作吗:
#!/usr/bin/perl -w
system(". /myfolder1/myfolder2/myScript.sh");
I have a script whose content simply exports a variable in linux.
export LD_LIBRARY_PATH=....
I want to run this script in my Perl script so whoever is running my Perl script will have their LD_LIBRARY_PATH
set. Can i just do this in the beginning of my Perl script:
#!/usr/bin/perl -w
system(". /myfolder1/myfolder2/myScript.sh");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当您运行它时,它将首先由
/bin/sh
执行,它能够将myScript.sh
加载到本地环境中。sh
然后exec
s Perl,它被告知从下一行继续。When you run this, it will first be executed by
/bin/sh
, which is capable of loadingmyScript.sh
into the local environment.sh
thenexec
s Perl, which is told to continue from the following line.这行不通。要更改 Perl 脚本内的环境(以及更改将传递到从 Perl 脚本内运行的命令的环境),请更改
%ENV
变量。This won't work. To change the environment inside your Perl script (and to change the environment that will be passed on to commands run from inside your Perl script), change the
%ENV
variable.这行不通。子 shell 无法操纵父进程的环境。
但是你可以让你的脚本回显你想要设置为 LD_LIBRARY_PATH 的字符串,然后在你的 Perl 脚本中你可以做这样的事情:
当然,一些错误检查也可能是一个好主意。
This won't work. There is no way for a subshell to manipulate the environment of the parent process.
But you could make your script echo the string you want to set as LD_LIBRARY_PATH and then from within your Perl script you could do something like that:
Of course, a bit of error checking might also be a good idea.
不会。您对孩子的环境改变不会影响父母。这意味着运行脚本不会影响 perl。 Perl 也不会影响调用它的 shell。您可以通过更改 特殊变量
%ENV
。如果该脚本中进行了某种不可重现的计算,则脚本可能应该只是echo
设置,而 Perl 可以在STDOUT
上获取该设置并使用它。No. Your environment changes made in a child cannot affect the parent. This means running a script will not affect perl. Also perl will not affect the shell from which it was called. You can edit the environment inside perl by changing the special variable
%ENV
. If there's some kind of unreproducible calculation done in that script, maybe the script should justecho
the setting and perl can pick that up onSTDOUT
and use it.几年前我遇到了类似的问题,并制作了一个小模块, Env::Sourced< /a>,这应该可以解决问题。
...
I had a similar problem a few years ago and whipped up a little module, Env::Sourced, that should do the trick.
...
另一种选择(除了直接在 Perl 的
%ENV
中进行更改之外)是进行所需 Perl 模块的更改,这样您就可以说:并让它在所有脚本中修改您的环境。这将使事后进行更改变得简单,而无需编辑每个脚本。
模块本身很简单,如下所示:
Another option (other than making the changes directly in Perl's
%ENV
) is to make the changes you want a Perl module, so that you can say:and have it modify your environment in all your scripts. It would make it simple to make changes after the fact that will not require editing every script.
The module itself will be simple, something like this:
那是行不通的。一个(令人不愉快的)替代方案可能是用 shell 脚本替换 /usr/bin/perl,该脚本首先执行您的脚本,然后执行 perl 可执行文件。
That won't work. An (unpleasant) alternative might be to replace /usr/bin/perl with a shell script that first executes your script and then executes the perl executable.
这不能以您尝试的方式完成。
它要么需要一个包装器 shell 脚本来设置 LD_LIBRARY_PATH,然后调用 Perl 脚本,要么执行该脚本的任何用户都需要首先正确设置 LD_LIBRARY_PATH。
如果选择后者,则可以通过编辑
/etc/profile
和/etc/cshrc
(适用于 ksh、sh、bash、csh 和 tcsh)shell 进行全局管理。然后,您可以在脚本中测试 LD_LIBRARY_PATH 的值,如果未设置/设置不正确,则向用户打印友好消息。或者,个人用户可以在其本地.profile
/.cshrc
文件中进行设置。注意:您尚未提供有关可能运行此程序的环境或用户的任何信息,因此用户也有可能将 LD_LIBRARY_PATH 设置为他们需要的内容。如果您在脚本中检查 LD_LIBRARY_PATH 是否有“良好”值,请记住可能已指定多个路径,因此您需要正确解析此环境变量。
This can't be done in the way you're trying to do this.
It either needs a wrapper shell script that sets
LD_LIBRARY_PATH
and then calls your perl script, or any user executing the script needs to haveLD_LIBRARY_PATH
set correctly in the first place.If doing the latter, then this can be managed globally by editing
/etc/profile
and/etc/cshrc
(for ksh, sh, bash, csh and tcsh) shells. You can then test for the value ofLD_LIBRARY_PATH
in your script and if not set/set incorrectly then print a friendly message to the user. Alternatively individual users can set this in their local.profile
/.cshrc
files.Note: you haven't given any information about the environment or useres that might run this, so there's also the possibility that users may set
LD_LIBRARY_PATH
to something they need. If you do checkLD_LIBRARY_PATH
for a "good" value in your script, then keep in mind that several paths may have been specified, so you will need to parse this environment variable properly.如果您可以在 Perl 脚本中找到正确的位置,则可以像我的示例一样工作:
并且它不需要我调用另一个脚本来设置环境变量。
If you can find the right place in your perl script, this works as in my example:
And it didn't require me to call another script to set the env var.
Env::Modify
模块至少解决了这个问题对于 POSIX-y 平台:The
Env::Modify
module addresses this issue, at least for POSIX-y platforms: