在设置环境变量时从ruby中取出shell

时间:2022-06-01 12:48:11

I need to shell out to a process while setting an environment variable for it. I tried this one-liner:

我需要在为它设置环境变量的同时对一个进程进行shell。我试过这个单行:

system "RBENV_VERSION=system ruby extconf.rb"

This syntax works in shell script but not from ruby. (Update: turns out this syntax works from ruby after all, but I failed to see its effect due to my particular use-case.)

此语法适用于shell脚本,但不适用于ruby。 (更新:事实证明这个语法毕竟是来自ruby,但由于我的特殊用例,我没有看到它的效果。)

So I'm doing this:

所以我这样做:

rbenv_version = ENV['RBENV_VERSION']
ENV['RBENV_VERSION'] = 'system'
begin
  system "ruby extconf.rb"
ensure
  ENV['RBENV_VERSION'] = rbenv_version
end

I'm forced to such a long expression because I don't want to override the environment variable permanently if it already had a value.

我*做了这么长的表达,因为如果它已经有了值,我不想永久地覆盖环境变量。

Anything shorter that comes to your mind?

想到什么更短的东西?

5 个解决方案

#1


66  

system({"MYVAR" => "42"}, "echo $MYVAR")

system accepts any arguments that Process.spawn accepts.

system接受Process.spawn接受的任何参数。

#2


6  

Ruby 1.9 includes Process::spawn which allows an environ hash to be provided.

Ruby 1.9包含Process :: spawn,它允许提供environ哈希。

Process::spawn is also the foundation for system, exec, popen, etc.
You can pass an environment to each.

Process :: spawn也是system,exec,popen等的基础。您可以将环境传递给每个。

Under Ruby 1.8, you may want to consider the POSIX::Spawn library,
which provides the same interfaces

在Ruby 1.8下,您可能需要考虑POSIX :: Spawn库,它提供相同的接口

#3


3  

Using your same approach, but wrapped up as a block method that temporarily modifies the environment (like the block form of Dir.chdir):

使用相同的方法,但包装为临时修改环境的块方法(如Dir.chdir的块形式):

def with_environment(variables={})
  if block_given?
    old_values = variables.map{ |k,v| [k,ENV[k]] }
    begin
       variables.each{ |k,v| ENV[k] = v }
       result = yield
    ensure
      old_values.each{ |k,v| ENV[k] = v }
    end
    result
  else
    variables.each{ |k,v| ENV[k] = v }
  end
end

with_environment 'RBENV_VERSION'=>'system' do
  `ruby extconf.rb`
end

#4


3  

Actually that worked for me.

实际上这对我有用。

shai@comp ~ » irb                                                                                                                                     
1.9.3p0 :001 > system %{SHAIGUITAR=exists ruby -e 'puts ENV["SHAIGUITAR"]'}
exists
 => true 

But if it doesn't, maybe you can try prepending "env" to whatever variable you need. E.g.

但如果没有,也许您可​​以尝试将“env”添加到您需要的任何变量中。例如。

system(%{env SHAIGUITAR=exists ruby bla.rb})

#5


2  

This may work?

这可能有用吗?

system <<-CMD
export VARNAME=123
other_command
CMD

#1


66  

system({"MYVAR" => "42"}, "echo $MYVAR")

system accepts any arguments that Process.spawn accepts.

system接受Process.spawn接受的任何参数。

#2


6  

Ruby 1.9 includes Process::spawn which allows an environ hash to be provided.

Ruby 1.9包含Process :: spawn,它允许提供environ哈希。

Process::spawn is also the foundation for system, exec, popen, etc.
You can pass an environment to each.

Process :: spawn也是system,exec,popen等的基础。您可以将环境传递给每个。

Under Ruby 1.8, you may want to consider the POSIX::Spawn library,
which provides the same interfaces

在Ruby 1.8下,您可能需要考虑POSIX :: Spawn库,它提供相同的接口

#3


3  

Using your same approach, but wrapped up as a block method that temporarily modifies the environment (like the block form of Dir.chdir):

使用相同的方法,但包装为临时修改环境的块方法(如Dir.chdir的块形式):

def with_environment(variables={})
  if block_given?
    old_values = variables.map{ |k,v| [k,ENV[k]] }
    begin
       variables.each{ |k,v| ENV[k] = v }
       result = yield
    ensure
      old_values.each{ |k,v| ENV[k] = v }
    end
    result
  else
    variables.each{ |k,v| ENV[k] = v }
  end
end

with_environment 'RBENV_VERSION'=>'system' do
  `ruby extconf.rb`
end

#4


3  

Actually that worked for me.

实际上这对我有用。

shai@comp ~ » irb                                                                                                                                     
1.9.3p0 :001 > system %{SHAIGUITAR=exists ruby -e 'puts ENV["SHAIGUITAR"]'}
exists
 => true 

But if it doesn't, maybe you can try prepending "env" to whatever variable you need. E.g.

但如果没有,也许您可​​以尝试将“env”添加到您需要的任何变量中。例如。

system(%{env SHAIGUITAR=exists ruby bla.rb})

#5


2  

This may work?

这可能有用吗?

system <<-CMD
export VARNAME=123
other_command
CMD