How to pipe output of a node CLI program to shell?

风流意气都作罢 提交于 2019-12-02 06:24:33

问题


I basically want to do something like this:

$ my-node-cli <some-param> | less

Note that less is just an example. I need it to work with any other *nix command.

More about the use case:
I wrote a node CLI package that searches some online resource and outputs results to the shell. Since the result set can be huge, client wants to do additional operations on it, e.g grep, head, tail, tee, ... anything really.

I searched far and wide and I only managed to find the way to pipe into node program, not out of. My current idea is to capture the right side of pipe when my program is called, then, after I obtain results, execute my results concatenated with pipe (and that part I remembered when I was called) using child_process.exec. Not sure whether that could work though?

Note that each time my program is called it's a new process, i.e. the program doesn't have it's own prompt.

Thanks


回答1:


All you need to do is output from STDOUT in your application. This will be sent to the next program if piped to it.

You can use plain old console.log() or the process.stdout stream.

It's up to the shell to handle stream redirection, not your application.



来源:https://stackoverflow.com/questions/30652243/how-to-pipe-output-of-a-node-cli-program-to-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!