Run a command line with custom environment

旧巷老猫 提交于 2019-11-30 17:42:51

Open.popen3 optionally accepts a hash as the first argument (in which case your command would be the second argument:

cmd = 'a_prog --arg ... --arg2 ...'
Open3.popen3({"MYVAR" => "a_value"}, "#{cmd}") { |i,o,e|
  output = o.read()
  error = e.read()
  # FIXME: don't want to *separate out* stderr like this
  repr = "$ #{cmd}\n#{output}"
}

Open uses Process.spawn to start the command, so you can look at the documentation for Process.spawn to see all of it's options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!