如何(终端)从 ruby​​ 脚本文件夹中的 cd [英] How to (terminal) cd in folder from ruby script

查看:47
本文介绍了如何(终端)从 ruby​​ 脚本文件夹中的 cd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以更改我正在执行 ruby​​ 脚本的当前终端目录.

I'd like to know if it's possible to change the current terminal directory from where I'm executing my ruby script.

例如,如果我在终端中从 $HOME 执行脚本,我想在脚本末尾的 $HOME/mydir 中执行 cd.

For example, if I'm executing the script from $HOME in the terminal, I would like to cd in $HOME/mydir at the end of the script.

我尝试了一些方法,但我总是在脚本开始的同一个目录中找到.

I've tried a couple of things, but I'm always finding in the same directorry where the script was started.

我尝试过的事情:

Dir.chdir( mydir )
%[cd mydir]

他们实际上确实改变了目录,但只是在脚本执行的线程中.当脚本结束时,我在终端中的当前位置仍然与启动脚本之前相同.

They actually do change directory, but only in the thread of the script execution. When the script ends, my current location in the terminal is still the same I was before launching the script.

我在 SO 中发现了类似的东西,但它是 Python 相关 和响应似乎是负面的.

I've found something similar in SO, but it's Python-related and response seems negative.

你可能会问为什么?

我目前正在参与一个命令行应用程序(使用 gli),作为起点,它需要一个项目文件夹.所以我的第一个命令看起来像:

I'm currently involved in a command line application (using gli) which, as starting point, needs a project folder. So my first command looks like:

$ myfoo new project

在当前目录中创建一个名为 project 的新文件夹.现在能够使用我需要的其他命令:

Which creates a new folder with name project in the current directory. Now to be able to work with the other commands I need to:

$ cd project
project$ myfoo domore

所以我只是认为在执行了 myfoo new project 之后在 project 中找到我自己会很好.

So I was simply thinking that it would be nice to found my self in project after having executed myfoo new project.

正如我所担心的,这并不容易(根本不可能).那么如何使用 bash 脚本来执行我想要的 ruby​​ 调用,而不是由 RubyGems 生成的标准文件呢?那可行吗?我傻吗?我应该使用 eval 吗?

As I was afraid, it's not that easy (not possible at all). So what about using a bash script which does the ruby calls that I want, in place of the standard file generated by RubyGems? Is that feasible? Am I foolish? Should I use eval?

感谢您的回答.但恐怕没人能接受.可能问题不是:)

Thanks for the answers. But I'm afraid that no one is acceptable. Probably the question wasn't :).

推荐答案

我怀疑是否有一种直接的方法可以做到这一点.

I doubt there is a straightforward way to do this.

当你从你的登录 shell 启动一个程序时,它在底层执行一个 fork/exec 这意味着一个新的(子)进程被创建(它有自己的环境,比如当前工作目录)并且它是标准输入/输出/错误流与(父)shell 进程相关联.因此,当您的脚本调用 Dir.chdir 时,它会更改脚本(子)进程的当前工作目录,而不是父进程.

When you launch a program from your login shell, underneath the hood it's doing a fork/exec which means a new (child) process is created (which has its own environment, such as current working directory) and it's standard input/output/error streams are associated with the (parent) shell process. So when your script calls Dir.chdir it changes the current working directory for the script (child) process, not the parent.

也许您的脚本可以在退出之前启动一个 shell,所以看起来您的终端已经更改了目录.虽然,在退出终端会话之前,您必须退出那个额外的 shell...

Perhaps your script could launch a new shell before it exits, so it looks like your terminal has changed directory. Although, you'd have to exit from that extra shell before exiting your terminal session...

这篇关于如何(终端)从 ruby​​ 脚本文件夹中的 cd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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