Ruby run shell command in a specific directory

后端 未结 6 1374
死守一世寂寞
死守一世寂寞 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:46

    You can use the block-version of Dir.chdir. Inside the block you are in the requested directory, after the Block you are still in the previous directory:

    Dir.chdir('mydir'){
      %x[#{cmd}]
    }
    

提交回复
热议问题