Ruby 在特定目录中运行 shell 命令

编程入门 行业动态 更新时间:2024-10-24 04:45:55
本文介绍了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 命令

本文发布于:2023-07-16 04:12:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   目录中   在特定   Ruby   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!