【问题标题】:How do you connect two (pipe stdin and stdout) Deno subprocesses?你如何连接两个(管道标准输入和标准输出)Deno 子进程?
【发布时间】:2020-11-08 07:47:21
【问题描述】:

根据API docs 判断,Deno 子进程(Deno.Process 的实例)可以接收四种标准输入类型之一,标准输出也是如此。但是,文档中没有提及如何将一个子进程的输出通过管道传输到另一个子进程的输入。我想要实现的是类似于基本的 UNIX 管道 (oneProcess | another),然后读取管道中第二个进程的输出。简单的运行

const someProcess = Deno.run({
  cmd: ["oneProcess firstParameter | another 2ndParameter"]
});

失败并出现以下错误:

error: Uncaught NotFound: No such file or directory (os error 2)

因为第一个参数(字符串)应该是可执行文件。

那么,Deno 将如何实现这一点,我们是否需要将"piped" 设置为子进程的输出和输入(分别),然后手动从一个进程读取和写入数据?

【问题讨论】:

  • 是的,第一个,假设我想运行一个进程,将该进程的输出通过管道传输到第二个,然后读取第二个进程的输出。
  • 知道了,现在我看到了错误,我的错。以为您遇到了oneProcess 不存在的问题。稍后我会发布答案。
  • 如果我的答案是你想要的,请告诉我。

标签: typescript deno


【解决方案1】:

您将获得NotFound: no such file or directory,因为传递给cmd 的值必须是二进制文件的路径。

第一个元素必须是二进制文件的路径

而且onProcess | another 不是二进制文件。


要使用 unix 管道 |,您可以运行 bash,然后写入 stdin

const p = Deno.run({
  cmd: ["bash"],
  stdout: "piped",
  stdin: "piped"
});

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const command = "echo -n yes | md5sum";
await p.stdin.write(encoder.encode(command));

await p.stdin.close();
const output = await p.output()
p.close();

console.log(decoder.decode(output)); // a6105c0a611b41b08f1209506350279e

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2012-03-13
    • 2013-07-16
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多