问题描述
我正在尝试通过 Perl 脚本以下列方式设置环境变量 LD_LIBRARY_PATH
:
I am trying to set an environment variable, LD_LIBRARY_PATH
, through a Perl script in the following way:
我在 /root
.profile
有一个 export
命令说:
export LD_LIBRARY_PATH=/
我的 Perl 脚本是 test.pl
并且它有:
My Perl script is test.pl
and it has:
#!/usr/bin/perl
system(". /root/.profile");
当我执行 ./test.pl
时,LD_LIBRARY_PATH
不会改变.
When I execute ./test.pl
, LD_LIBRARY_PATH
doesn't change.
我做错了什么?
推荐答案
您当前的脚本甚至不会更改 Perl 脚本本身中的环境变量.相反,它调用 shell 作为子进程;该 shell 进程执行 ./root/.profile
,仅在该 shell 进程中更新 $LD_LIBRARY_PATH
.
Your current script doesn't even change an environment variable in the Perl script itself. Rather, it invokes a shell as a subprocess; that shell process executes . /root/.profile
, which updates $LD_LIBRARY_PATH
only in that shell process.
您可以通过更新 %ENV
来更改 Perl 脚本中的环境变量(更准确地说,是在运行 Perl 脚本的过程中):
You can change an environment variable in a Perl script (more precisely, in the process running the Perl script) by updating %ENV
:
$ENV{LD_LIBRARY_PATH} = '/'; # or some more reasonable value
正如 perldoc -v %ENV
所说:
%ENV
散列 %ENV
包含您当前的环境.在ENV"中设置一个值会更改您随后fork()
"关闭的任何子进程的环境.
但这可能仍然无法满足您的需求;它不会(也不能)影响调用 Perl 脚本(您的交互式 shell)的进程的环境,只会影响 Perl 进程本身及其调用的任何内容.
But that probably still won't do what you want; it won't (and can't) affect the environment of the process that invokes the Perl script (your interactive shell), only the Perl process itself and anything it invokes.
我假设您想在当前交互式 shell 进程中更新 $LD_LIBRARY_PATH
.为此,您可以让 Perl 脚本打印一个 shell 命令来更新 $LD_LIBRARY_PATH
.然后,您可以执行它然后评估它的输出,而不是简单地运行 Perl 脚本.例如:
I'll assume you want to update $LD_LIBRARY_PATH
in your current interactive shell process. To do that, you can have you Perl script print a shell command that will update $LD_LIBRARY_PATH
. Then, rather than simply running your Perl script, you can execute it and then evaluate its output. For example:
$ cat env.pl
#!/usr/bin/perl
use strict;
use warnings;
print "export LD_LIBRARY_PATH=/
";
$ ./env.pl # just prints the command without executing it
export LD_LIBRARY_PATH=/
$ eval $(./env.pl) # executes the command in the current shell
$ echo $LD_LIBRARY_PATH
/
$
这假设您当前的 shell 是 bash 或类似的东西.
This assumes that your current shell is bash or something similar.
另一个选项:修改%ENV
后,您的Perl 脚本可以调用另一个命令,甚至是一个新的交互式shell.新进程将从 Perl 脚本继承其环境.不过,这可能有点麻烦;例如,如果新进程是一个交互式 shell,它不会从父 shell 继承未导出的变量或历史.
Another option: After modifying %ENV
, your Perl script can invoke another command, even a new interactive shell. The new process will inherit its environment from the Perl script. This can be a bit cumbersome, though; for example, if the new process is an interactive shell, it won't inherit unexported variables or history from the parent shell.
(一个注释,与您的问题没有直接关系:您正在使用 /root/.profile
的事实意味着您正在以 root
的身份做事(超级用户).这可能很危险.使用 root
帐户(通过登录或通过 sudo
仅用于实际需要 root 权限的事情.对于其他任何事情,请使用个人用户帐户.
(One note, not directly related to your question: The fact that you're messing with /root/.profile
implies that you're doing things as root
(superuser). This can be dangerous. Use the root
account (either by logging into it or via sudo
only for things that actually need root privileges. For anything else, use a personal user account.
这篇关于通过 Perl 脚本设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!