如何将节点CLI程序的输出通过管道传输到Shell? [英] How to pipe output of a node CLI program to shell?
问题描述
我基本上想做这样的事情:
$ my-node-cli <some-param> | less
请注意,less
仅是示例.我需要它与任何其他* nix命令一起使用.
有关用例的更多信息:
我编写了一个节点CLI程序包,该程序包搜索一些在线资源并将结果输出到Shell.由于结果集可能非常庞大,因此客户希望对它进行其他操作,例如grep
,head
,tail
,tee
,...等等.
我进行了广泛搜索,只设法找到了将导入节点程序的方法,而不是退出的方法.我当前的想法是在调用程序时捕获管道的右侧,然后在获得结果后,使用child_process.exec
执行与管道(以及在调用时记得的那部分)连接的结果.不知道这是否行得通?
请注意,每次调用我的程序都是一个新过程,即该程序没有自己的提示符.
谢谢
所有您需要做的就是从应用程序中的STDOUT输出.如果通过管道传输到下一个程序,它将被发送到下一个程序.
您可以使用普通的console.log()
或 process.stdout
流./p>
由shell处理流重定向,而不是由您的应用程序决定.
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
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.
这篇关于如何将节点CLI程序的输出通过管道传输到Shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!