如何将环境设置导入到我的 Perl 程序中?

发布于 2024-08-08 20:07:53 字数 273 浏览 4 评论 0原文

我有一个脚本,其内容只是导出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

够运 2024-08-15 20:07:53
#!/bin/sh
. /myfolder1/myfolder2/myScript.sh
exec perl -wxS "$0" "$@"
#!/usr/bin/perl -w
# .. the rest of your script as normal

当您运行它时,它将首先由 /bin/sh 执行,它能够将 myScript.sh 加载到本地环境中。 sh 然后 execs Perl,它被告知从下一行继续。

#!/bin/sh
. /myfolder1/myfolder2/myScript.sh
exec perl -wxS "$0" "$@"
#!/usr/bin/perl -w
# .. the rest of your script as normal

When you run this, it will first be executed by /bin/sh, which is capable of loading myScript.sh into the local environment. sh then execs Perl, which is told to continue from the following line.

紙鸢 2024-08-15 20:07:53

这行不通。要更改 Perl 脚本内的环境(以及更改将传递到从 Perl 脚本内运行的命令的环境),请更改 %ENV 变量。

$ENV{"LD_LIBRARY_PATH"} = ... ;

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.

$ENV{"LD_LIBRARY_PATH"} = ... ;
黑凤梨 2024-08-15 20:07:53

这行不通。子 shell 无法操纵父进程的环境。

但是你可以让你的脚本回显你想要设置为 LD_LIBRARY_PATH 的字符串,然后在你的 Perl 脚本中你可以做这样的事情:

$ENV{LD_LIBRARY_PATH} = `path/to/your/script.sh`;

当然,一些错误检查也可能是一个好主意。

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:

$ENV{LD_LIBRARY_PATH} = `path/to/your/script.sh`;

Of course, a bit of error checking might also be a good idea.

最终幸福 2024-08-15 20:07:53

不会。您对孩子的环境改变不会影响父母。这意味着运行脚本不会影响 perl。 Perl 也不会影响调用它的 shell。您可以通过更改 特殊变量 %ENV。如果该脚本中进行了某种不可重现的计算,则脚本可能应该只是echo 设置,而 Perl 可以在 STDOUT 上获取该设置并使用它。

我在 Perl 脚本中{更改了目录,修改了我的环境}。为什么当我退出脚本时更改就消失了?如何让我的更改可见?

Unix 从最严格的意义上来说,这是不可能的——脚本执行
作为与 shell 不同的进程
它是从开始的。更改为
过程并没有体现在其
父级,仅在其自己的子级中
更改后创建。

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 just echo the setting and perl can pick that up on STDOUT and use it.

I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

Unix In the strictest sense, it can't be done -- the script executes
as a different process from the shell
it was started from. Changes to a
process are not reflected in its
parent, only in its own children
created after the change.

掐死时间 2024-08-15 20:07:53

几年前我遇到了类似的问题,并制作了一个小模块, Env::Sourced< /a>,这应该可以解决问题。

 use Env::Sourced qw(/myfolder1/myfolder2/myScript.sh);

...

I had a similar problem a few years ago and whipped up a little module, Env::Sourced, that should do the trick.

 use Env::Sourced qw(/myfolder1/myfolder2/myScript.sh);

...

无声无音无过去 2024-08-15 20:07:53

另一种选择(除了直接在 Perl 的 %ENV 中进行更改之外)是进行所需 Perl 模块的更改,这样您就可以说:

use MyEnvironment;

并让它在所有脚本中修改您的环境。这将使事后进行更改变得简单,而无需编辑每个脚本。

模块本身很简单,如下所示:

package MyEnvironment;

$ENV{LD_LIBRARY_PATH} .= ":/some/path/you/want/appended";
# Any other changes you want here.

1;

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:

use MyEnvironment;

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:

package MyEnvironment;

$ENV{LD_LIBRARY_PATH} .= ":/some/path/you/want/appended";
# Any other changes you want here.

1;
岁月流歌 2024-08-15 20:07:53

那是行不通的。一个(令人不愉快的)替代方案可能是用 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.

孤单情人 2024-08-15 20:07:53

这不能以您尝试的方式完成。

它要么需要一个包装器 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 have LD_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 of LD_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 check LD_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.

只怪假的太真实 2024-08-15 20:07:53

如果您可以在 Perl 脚本中找到正确的位置,则可以像我的示例一样工作:

$ENV{"LD_LIBRARY_PATH"} = "/oracle/product/10g/lib";

并且它不需要我调用另一个脚本来设置环境变量。

If you can find the right place in your perl script, this works as in my example:

$ENV{"LD_LIBRARY_PATH"} = "/oracle/product/10g/lib";

And it didn't require me to call another script to set the env var.

皇甫轩 2024-08-15 20:07:53

Env::Modify 模块至少解决了这个问题对于 POSIX-y 平台:

use Env::Modify 'source';
source("/myfolder1/myfolder2/myScript.sh");
... environment settings from myScript.sh are now available to Perl ...

The Env::Modify module addresses this issue, at least for POSIX-y platforms:

use Env::Modify 'source';
source("/myfolder1/myfolder2/myScript.sh");
... environment settings from myScript.sh are now available to Perl ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文