如何在Perl脚本中导出shell变量? [英] How to export a shell variable within a Perl script?

查看:253
本文介绍了如何在Perl脚本中导出shell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用一个Perl脚本输入一个shell脚本,其中包含一个shell变量列表,它是在进入编程环境之前执行的。编程环境:

  system(environment_defaults.sh); 
system(obe);

但是当我进入环境时,变量没有设置。

解决方案

当您调用第二个命令时,在第一个命令中修改的环境中未完成。实际上,由于用于调用environment_defaults.sh的shell已经退出,所以没有第一个命令的环境。



为了保持第二个命令的上下文,在同一个shell中调用它们:

  system(source environment_defaults.sh && obe); 

请注意,您需要使用 source 为了在当前的 shell中执行其操作,而不是调用一个新的shell来执行它们。



或者,修改你的在每个shell的开始环境(例如.bash_profile,如果使用bash),或者使你的环境变量在perl本身中变化:

  $ ENV {FOO} =hello; 
system('echo $ FOO');


I have a shell script, with a list of shell variables, which is executed before entering a programming environment.

I want to use a Perl script to enter the programming environment:

system("environment_defaults.sh");
system("obe");

But when I enter the environment the variables are not set.

解决方案

When you call your second command, it's not done in the environment you modified in the first command. In fact, there is no environment remaining from the first command, because the shell used to invoke "environment_defaults.sh" has already exited.

To keep the context of the first command in the second, invoke them in the same shell:

system("source environment_defaults.sh && obe");

Note that you need to invoke the shell script with source in order to perform its actions in the current shell, rather than invoking a new shell to execute them.

Alternatively, modify your environment at the beginning of every shell (e.g. with .bash_profile, if using bash), or make your environment variable changes in perl itself:

$ENV{FOO} = "hello";
system('echo $FOO');

这篇关于如何在Perl脚本中导出shell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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