Ruby 中命令执行的问题
我正在使用系统命令
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要“链接”两个命令,以便它们在同一个 系统子shell。也就是说,如果您在单独的 ruby“系统”调用中执行两个命令,那么它们将修改不直接相关的单独子程序的环境。
在上面的示例中,“program.exe”不知道“env.bat”是否通过添加新的环境变量(例如)更改了环境,因为它们在单独的、不相关的进程中运行。
但在此示例中,只要“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.
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.
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".