通过一个Perl脚本设置环境变量 [英] Setting an environment variable through a Perl script

查看:2338
本文介绍了通过一个Perl脚本设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置环境变量, LD_LIBRARY_PATH ,通过以下方式Perl脚本:

I am trying to set an environment variable, LD_LIBRARY_PATH, through a Perl script in the following way:

我已经创建 .profile文件 /根

.profile文件有一个导出命令说:

export LD_LIBRARY_PATH=/

我的Perl脚本是 test.pl ,它有:

#!/usr/bin/perl
system(". /root/.profile");

当我执行 ./ test.pl LD_LIBRARY_PATH 不会改变。

我在做什么错了?

推荐答案

您当前的脚本甚至没有在Perl脚本本身改变一个环境变量。相反,它会调用一个shell子进程;壳牌进程执行。 /root/.profile ,其中更新 $ LD_LIBRARY_PATH 仅在该shell进程。

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.

您可以通过更新改变一个Perl脚本环境变量(更多precisely,在运行的Perl脚本的进程)%ENV

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数值改变时所有子环境处理您以后叉()关闭。

%ENV The hash %ENV contains your current environment. Setting a value in "ENV" changes the environment for any child processes you subsequently "fork()" off.

但是,你想要的东西可能仍然不会做;它不会(也不能)影响调用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.

我假设你想更新 $ LD_LIBRARY_PATH 在当前交互shell进程。要做到这一点,你可以有你的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=/\n";
$ ./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变量或历史。

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权限。为别的,使用个人用户帐户。

(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脚本设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆