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

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

问题描述

我有一个 shell 脚本,其中包含一个 shell 变量列表,在进入编程环境之前执行.

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

我想用Perl脚本进入编程环境:

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.

推荐答案

当你调用你的第二个命令时,它并没有在你在第一个命令中修改的环境中完成.事实上,没有第一个命令留下的环境,因为用来调用environment_defaults.sh"的shell已经退出了.

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.

要在第二个命令中保留第一个命令的上下文,请在同一个 shell 中调用它们:

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

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

请注意,您需要使用 source 调用 shell 脚本,以便在 当前 shell 中执行其操作,而不是调用新的 shell 来执行它们.

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.

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

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天全站免登陆