bash 从 perl 更改 PATH
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13372310/
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
Changing the PATH from perl
提问by leonsas
So I need to call some perl scripts from another script. To make it work from the terminal I need to go to bash and then change the PATH:
所以我需要从另一个脚本调用一些 perl 脚本。要使其从终端工作,我需要转到 bash,然后更改 PATH:
 export PATH=$PATH:/home/lsk250/www/portfolio
Now I need to run this as a CGI and invoke it from a browser, but obviously when I try to invoke the script from the browser, the PATH is not set so the script can't find the programs needed. How can I change the PATH from inside a perl script? I tried the following, without success:
现在我需要将它作为 CGI 运行并从浏览器调用它,但很明显,当我尝试从浏览器调用脚本时,PATH 未设置,因此脚本找不到所需的程序。如何从 perl 脚本内部更改 PATH?我尝试了以下方法,但没有成功:
system "export PATH=$PATH:/home/lsk250/www/portfolio";
and
和
$ENV{PATH} = '/home/lsk250/www/portfolio';
exec 'env',cwd().'/'.$ENV{PATH} = "$ENV{PATH}:/home/lsk250/www/portfolio";
,@ARGV;
Any ideas?
有任何想法吗?
回答by ikegami
export PATH="$PATH:/home/lsk250/www/portfolio"
is indeed correct. It's Perl's equivalent to sh's
确实是正确的。它的Perl的等效sh的
my $myPath = "/foo/bar";
exec("PATH=$PATH:$myPath command");
Contrary to what you said, the subsequent execwill use this path to locate the executable, and that executable will see the updated PATH.
与您所说的相反,后续exec将使用此路径来定位可执行文件,并且该可执行文件将看到更新后的 PATH。
You seem to have misdiagnosed the problem. What is actually happpening? Is the execfailing? If so, what error did it return?
你似乎误诊了这个问题。实际发生了什么?是exec失败的?如果是这样,它返回了什么错误?
回答by Gilles Quenot
2 solutions :
2个解决方案:
my $myPath = "/foo/bar";
exec("$myPath/command");
or
或者
##代码##
