开发者

Problem in command execution in Ruby

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 envi开发者_如何学JAVAronment that I load in the ruby shell is used in the commands that I execute afterwards.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜