你如何连接两个(管道标准输入和标准输出)Deno 子进程?
How do you connect two (pipe stdin and stdout) Deno subprocesses?
根据 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
根据 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