Ruby运行shell命令在特定目录中 [英] Ruby run shell command in a specific directory

查看:112
本文介绍了Ruby运行shell命令在特定目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Ruby中运行shell命令:

 %x [#{cmd}] $ b $但是,如何指定运行此命令的目录?







$ b b

有类似的方法,类似于 subprocess.Popen 在Python中:

  subprocess.Popen(r'c:\mytool\tool.exe',cwd = r'd:\test\local')


$ b

谢谢!

解决方案

块版本 Dir.chdir 。在块中你在请求的目录中,之后你仍然在上一个目录:

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


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 similar way of shelling out, similar to subprocess.Popen in Python:

subprocess.Popen(r'c:\mytool\tool.exe', cwd=r'd:\test\local')

Thanks!

解决方案

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}]
}

这篇关于Ruby运行shell命令在特定目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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