如何在Ruby脚本中为命令shell导入环境变量? [英] How do I source environment variables for a command shell in a Ruby script?

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

问题描述

我正在尝试从我的红宝石程序中运行一些第三方bash脚本。



在我运行它们之前,我们需要我来源文件。在命令行上,它一切正常,但在Ruby中它不起作用。我发现系统命令将打开一个新的子shell进程,任何源代码都将完成,并且不能从运行Ruby脚本的父shell进程看到。当系统调用结束时,子shell也被杀死。



如何解决这个问题?

解决方案

  $ source whatever.sh 
$ set>变量txt

然后在Ruby中:

  File.readlines(variables.txt)。each do | line | 
values = line.split(=)
ENV [values [0]] = values [1]
end

运行完毕后,您的环境应该很好。


I'm trying to run some third party bash scripts from within my ruby program.

Before I can run them they require me to source a file. On the command line it all works fine but within Ruby it doesn't work. I've found out that system commands will open a new child shell process and any sourcing will be done in that and can't be seen from the parent shell process running the Ruby script. When the system call ends, the child shell is also killed.

How do i get round this problem?

解决方案

Do this:

$ source whatever.sh
$ set > variables.txt

And then in Ruby:

File.readlines("variables.txt").each do |line|
  values = line.split("=")
  ENV[values[0]] = values[1]
end

After you've ran this, your environment should be good to go.

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

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