从 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"
作为子进程的输出和输入(分别),然后手动从一个到另一个读取和写入数据?
最佳答案
您收到 NotFound: no such file or directory
因为传递给 cmd
的值必须是二进制文件的路径。
the first element needs to be a path to the binary
和
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
关于typescript - 你如何连接两个(管道标准输入和标准输出)Deno 子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62088065/