javascript 如何使用 Unix 将 Node.js 脚本通过管道连接在一起 | 管道(在命令行上)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16349706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pipe Node.js scripts together using the Unix | pipe (on the command line)?
提问by Lance Pollard
I see how to pipe stuff together using Node.js streams, but how do you pipe multiple scripts together using the Unix |
, given that some of these scripts can be async?
我看到了如何使用 Node.js 流将内容连接在一起,但是您如何使用 Unix 将多个脚本连接在一起|
,因为其中一些脚本可以是异步的?
$ ./a.js | ./b.js
Example:
例子:
a.js(chmod 0755)
a.js(chmod 0755)
#!/usr/bin/env node
setTimeout(function(){
console.log(JSON.stringify({ foo: 'bar' }));
}, 10);
b.js(chmod 0755)
b.js(chmod 0755)
#!/usr/bin/env node
console.log(process.argv);
This is the output:
这是输出:
$ ./a.js | ./b.js
[ 'node', '/Users/viatropos/tests/b.js' ]
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:883:11)
at Object.afterWrite (net.js:700:19)
At first glance it seems like there's a lot going wrong, so not really sure where to start. Is there a way to get this to work? The end goal is to be able to take the console.log
output from ./a.js
and use it in ./b.js
. The reason is, most of the time these scripts will be run one at a time, but sometimes it would be nice to be able to pipe them together, so ideally the system should be able to handle both cases.
乍一看似乎有很多问题,所以不确定从哪里开始。有没有办法让它发挥作用?最终的目标是能够采取console.log
输出./a.js
,并使用它./b.js
。原因是,大多数时候这些脚本一次运行一个,但有时能够将它们通过管道连接在一起会很好,因此理想情况下,系统应该能够处理这两种情况。
回答by Aaron Dufour
The problem is that your b.js
immediately ends and closes its standard in, which causes an error in a.js
, because its standard out got shut off and you didn't handle that possibility. You have two options: handle stdout closing in a.js
or accept input in b.js
.
问题是您b.js
立即结束并关闭其标准输入,这会导致 错误a.js
,因为它的标准输出被关闭,而您没有处理这种可能性。您有两个选择:处理标准输出关闭a.js
或接受输入b.js
。
Fixing a.js
:
修复a.js
:
process.on("SIGPIPE", process.exit);
If you add that line, it'll just give up when there's no one reading its output anymore. There are probably better things to do on SIGPIPE depending on what your program is doing, but the key is to stop console.log
ing.
如果你添加那一行,当没有人再读它的输出时它就会放弃。根据您的程序在做什么,在 SIGPIPE 上可能有更好的事情要做,但关键是停止console.log
ing。
Fixing b.js
:
修复b.js
:
#!/usr/bin/env node
var stdin = process.openStdin();
var data = "";
stdin.on('data', function(chunk) {
data += chunk;
});
stdin.on('end', function() {
console.log("DATA:\n" + data + "\nEND DATA");
});
Of course, you don't haveto do anything with that data. They key is to have something that keeps the process running; if you're piping to it, stdin.on('data', fx)
seems like a useful thing to do.
当然,你不具备做与数据什么。他们的关键是要有一些东西来保持流程的运行;如果你正在使用它,这stdin.on('data', fx)
似乎是一件很有用的事情。
Remember, either one of those will prevent that error. I expect the second to be most useful if you're planning on piping between programs.
请记住,其中任何一个都可以防止该错误。如果您计划在程序之间进行管道传输,我希望第二个最有用。