如何使用Unix将Node.js脚本一起管道管道(在命令行上)? [英] How to pipe Node.js scripts together using the Unix | pipe (on the command line)?

查看:80
本文介绍了如何使用Unix将Node.js脚本一起管道管道(在命令行上)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了如何使用Node.js流将内容组合在一起,但是鉴于某些脚本可以异步使用,如何使用Unix |将多个脚本一起组合?

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?

$ ./a.js | ./b.js

示例:

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);

这是输出:

$ ./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)

乍一看,似乎出了很多问题,所以不太确定从哪里开始.有没有办法让它工作?最终目标是能够从./a.js获取console.log输出并在./b.js中使用它.原因是,在大多数情况下,这些脚本一次只能运行一次,但有时能够将它们通过管道传输会很好,因此,理想情况下,系统应该能够处理这两种情况.

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.

推荐答案

问题是您的b.js立即结束并关闭其标准输入,这会导致a.js中的错误,因为其标准输出已关闭并且您没有处理这种可能性.您有两个选择:处理a.js中的stdout关闭或接受b.js中的输入.

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.

修复a.js:

process.on("SIGPIPE", process.exit);

如果添加该行,则在没有人再读取其输出时,它将放弃.取决于您的程序正在做的事情,在SIGPIPE上可能有更好的事情要做,但是关键是停止console.log ing.

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.loging.

修复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");
});

当然,您没有没有对该数据进行任何处理.他们的关键是要有一些东西可以保持流程的运行.如果您正在使用它,stdin.on('data', fx)似乎是一件有用的事情.

Of course, you don't have to 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.

请记住,其中任何一个都可以防止该错误.如果您计划在程序之间进行管道传输,我希望第二个方法最有用.

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.

这篇关于如何使用Unix将Node.js脚本一起管道管道(在命令行上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆