在bash、zsh、ksh 88、ksh 93和类似的shell中,可以简单地生成一个文件名(根据平台的需要,可以实现为/dev/fd
条目或命名管道),其中一段给定的代码在子shell中执行。
我对在Ruby解释器中做同样的事情很感兴趣--执行一个子进程,其中一个参数是一个文件名,当读取该文件名时,将输出当前Ruby解释器或子进程中运行的代码。
内容是安全敏感的,因此写入临时文件并不理想。
bash中的等效代码:
./run-program --password-file=<(printf '%s' "$password")
.运行时,将(在Linux或其他具有/dev/fd/
的平台上)调用如下内容:
./run-program --password-file=/dev/fd/5
.其中/dev/fd/5
可以被读取(仅一次,作为流)以检索扩展$password
的结果。
在Ruby中实现相同调用约定的最佳方法是什么?
3条答案
按热度按时间jm2pwxwz1#
IO.popen
的作用与shell中的>()
或<()
相同--它创建一个未命名的单向管道,您可以对该管道进行读或写。基本上,我认为你会想使用unamed管道。另一种常见的模式是使用
IO.pipe
创建管道对,分叉一个子进程,由于分叉共享文件描述符,您的子进程和父进程基本上将通过该管道对连接,您可以拥有任意多个管道对。46scxncf2#
基本的答案可能是从Ruby的
Kernel#spawn
方法中构建一些东西。这将允许您启动另一个进程,并控制子进程使用哪些文件对象进行输入和输出。请注意,处理这可能是棘手的;手边有一本好Unix应用程序编程参考书是很有帮助的。Ruby实际上只是在一些低级Unix/C API周围放置了一个薄薄的 Package 。
mi7gmzs63#
您可以使用
IO.popen
将密码传递给cat
程序。