本文介绍了Ruby 在特定目录中运行 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何在 Ruby 中运行 shell 命令,例如:
I know how to run a shell command in Ruby like:
%x[#{cmd}]但是,我该如何指定一个目录来运行这个命令呢?
But, how do I specify a directory to run this command?
有没有类似的脱壳方式,类似于Python中的subprocess.Popen:
Is there a similar way of shelling out, similar to subprocess.Popen in Python:
subprocess.Popen(r'c:mytool ool.exe', cwd=r'd: estlocal')谢谢!
推荐答案您可以使用 Dir.chdir 的块版本.在块内你在请求的目录中,在块之后你仍然在上一个目录中:
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 命令
发布评论