Ruby run shell command in a specific directory

后端 未结 6 1373
死守一世寂寞
死守一世寂寞 2020-12-01 07:23

I know how to run a shell command in Ruby like:

%x[#{cmd}]

But, how do I specify a directory to run this command?

Is there a simila

6条回答
  •  孤城傲影
    2020-12-01 07:54

    Ruby 1.9.3 (blocking call):

    require 'open3'
    Open3.popen3("pwd", :chdir=>"/") {|i,o,e,t|
      p o.read.chomp #=> "/"
    }
    
    Dir.pwd #=> "/home/abe"
    

提交回复
热议问题