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

编程入门 行业动态 更新时间:2024-10-24 06:30:23
本文介绍了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命令在特定目录中

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

发布评论

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

>www.elefans.com

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