Ruby 中命令执行的问题

发布于 2024-11-02 19:37:24 字数 266 浏览 0 评论 0原文

我正在使用系统命令在Ruby中执行命令,但遇到以下问题:

我使用命令Environment.bat加载环境,我想执行第二个命令,它利用我在上一个命令中成功设置的环境。然而,似乎早点加载环境根本没有任何效果。

如何解决这个问题,以便我在 ruby​​ shell 中加载的环境用于我之后执行的命令中。

I am executing commands in Ruby using system command, but I am facing the following problem:

I load an environment using the command Environment.bat, and I want to execute the second command which makes use of the environment that I have set up successfully in the previous command. However, it seems as if loading the environment earlier does not have any effect at all.

How to solve this problem so that the environment that I load in the ruby shell is used in the commands that I execute afterwards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-11-09 19:37:24

您可能需要“链接”两个命令,以便它们在同一个 系统子shell。也就是说,如果您在单独的 ruby​​“系统”调用中执行两个命令,那么它们将修改不直接相关的单独子程序的环境。

system("env.bat") # Executes in child process 1.
system("program.exe") # Executes in child process 2.

在上面的示例中,“program.exe”不知道“env.bat”是否通过添加新的环境变量(例如)更改了环境,因为它们在单独的、不相关的进程中运行。

system("env.bat && program.exe") # Both in the same child process.

但在此示例中,只要“env.bat”不退出并出现错误代码,这两个命令就会在同一子 shell 进程中依次运行。在这种情况下,“program.exe”将有权访问“env.bat”设置的任何新环境变量。

You might need to "chain" your two commands so that they get executed in the same system subshell. That is, if you're executing two commands in separate ruby "system" calls then they are modifying the environments of separate child programs which are not directly related.

system("env.bat") # Executes in child process 1.
system("program.exe") # Executes in child process 2.

In the above example, "program.exe" wouldn't know if "env.bat" had changed the environment by adding a new environment variable (for example) since they run in separate, unrelated processes.

system("env.bat && program.exe") # Both in the same child process.

But in this example the two commands are run in the same subshell process, one after the other, as long as "env.bat" doesn't exit with an error code. In this case "program.exe" would have access to any new environment variables set by "env.bat".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文