本文介绍了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命令在特定目录中
发布评论