从 ruby​​ 脚本更改调用 shell 的当前工作目录

发布于 2024-08-24 15:42:25 字数 444 浏览 7 评论 0原文

我正在编写一个简单的 ruby​​ 沙箱命令行实用程序,用于将目录从远程文件系统复制并解压缩到本地临时目录,以便解压缩它们并让用户编辑文件。我使用 Dir.mktmpdir 作为默认的暂存目录,这提供了一个非常丑陋的路径(例如:/var/folders/zz/zzzivhrRnAmviuee+++1vE+++yo/-Tmp-/d20100311-70034-abz5zj)

我希望复制和解压缩脚本的最后一个操作将调用 shell cd 到新的临时目录中,以便人们可以轻松访问它,但我不知道如何更改调用 shell 的 PWD。一种可能性是让实用程序打印出 stdout 的新路径,然后将脚本作为子 shell 的一部分运行(即 cd $(sandbox my_dir) ),但我想打印出进度复制并解压缩,因为它可能需要长达 10 分钟的时间,所以这不起作用。我应该将它放在预先确定的、易于查找的临时目录中吗?有人有更好的建议吗?预先感谢您的帮助。 -埃里克

I'm writing a simple ruby sandbox command-line utility to copy and unzip directories from a remote filesystem to a local scratch directory in order to unzip them and let users edit the files. I'm using Dir.mktmpdir as the default scratch directory, which gives a really ugly path (for example: /var/folders/zz/zzzivhrRnAmviuee+++1vE+++yo/-Tmp-/d20100311-70034-abz5zj)

I'd like the last action of the copy-and-unzip script to cd the calling shell into the new scratch directory so people can access it easily, but I can't figure out how to change the PWD of the calling shell. One possibility is to have the utility print out the new path to stdout and then run the script as part of a subshell (i.e. cd $(sandbox my_dir) ), but I want to print out progress on the copy-and-unzipping since it can take up to 10 minutes, so this won't work. Should I just have it go to a pre-determined, easy-to-find scratch directory? Does anyone have a better suggestion? Thanks in advance for your help. -Erik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光沙漏 2024-08-31 15:42:25

你可能会死在你的暂存目录中的一个新 shell 中,但我无法理解这一切的意义。如果您的用户要编辑这些文件,那么我想他们要么想要保留它们,要么您计划使用修改后的文件。在第一种情况下,将文件放在合理的位置,您不必担心用户找到它们(当前工作目录)。在第二种情况下,为您的用户生成一个位于暂存目录中的子 shell,并指示他们编辑文件。当子 shell 退出时,您可以继续从同一个 ruby​​ 程序处理文件。

You could die into a new shell that sits at your scratch dir, but I am having trouble understanding the point of all this. If your users are going to edit these files, then i suppose they either want to keep them or you are planning on working with the modified files. In the first case, put the files in a sensible location and you won't have to worry about your users finding them (current working directory). In the second case, spawn a subshell for your users that sits in the scratch dir and instruct them to edit the files. When the subshell exits you can continue to process your files from the same ruby program.

暮年 2024-08-31 15:42:25

进程无法更改其父进程的环境,因此您必须在父进程中执行一些操作。

您可以让您的用户获取 shell 脚本,该脚本调用 ruby​​ 程序和 cd 的某个地方。当您使用 shell 的 source 命令(.source)时,会影响当前进程。

A process cannot change the environment of its parent process, so you will have to do some action in the parent.

You could have your users source a shell script, and that script calls the ruby program and cd's somewhere. When you use your shell's source command (. or source), that affects the current process.

你没皮卡萌 2024-08-31 15:42:25

格伦·杰克曼指出,子进程无法更改其父进程的当前工作目录(或环境变量等)。除非您使用调试器计算违反父进程的情况……

命令替换的一部分运行的进程可以写入 stderr 以向用户提供输出,同时命令替换捕获 stdout 输出:

cd "$(sandbox whatever)"

作为 > 命令将其大部分输出发送到 stderr,仅将最终的“directory to chdir to”发送到 stdout。如果sandbox是一个shell脚本,你可以这样做:

#!/bin/sh
newdir="$(mktemp -dt sandobox)"
{
# do whatever, in a sub-shell
# use a group command (or change the braces to parens to use a sub-shell) to
# capture all stdout and send it to stderr 
} 1>&2
echo "$newdir"

我确信Ruby中也可以完成类似的事情。

As, glenn jackman notes, a child process can not change its parent's current working directory (or environment variables, etc.). Unless you count violating the parent process with a debugger…

A process run as part of a command substitution can write to stderr to provide output to the user while the stdout output is captured by the command substitution:

cd "$(sandbox whatever)"

Where the sandbox command sends most of its output to stderr, and just the final ‘directory to chdir to’ to stdout. If sandbox was a shell script, you could do it like this:

#!/bin/sh
newdir="$(mktemp -dt sandobox)"
{
# do whatever, in a sub-shell
# use a group command (or change the braces to parens to use a sub-shell) to
# capture all stdout and send it to stderr 
} 1>&2
echo "$newdir"

I am sure something equivalent could be done in Ruby.

和影子一齐双人舞 2024-08-31 15:42:25

首先从包装 shell 脚本创建临时目录,然后将其作为命令行参数传递给 Ruby 脚本。

Create your temp directory from your wrapping shell script first and then pass it as a command line argument to your Ruby script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文