node js interact with shell application

拥有回忆 提交于 2019-12-18 12:35:29

问题


There are plenty of node js examples on online about how to spawn a child process and then catch the result as a string for your own processing.

But...

I want to 'interact' with a child process. For example, how would I write a node js application than starts by calling 'python' and then types a statement '1+1', lets me catch the result '2', before proceeding to type another arbitrary statement '4+4'?

(And by 'type' I'm assuming it will require streaming data to the stdin that the process uses).


回答1:


var child = require('child_process');
var ps = child.spawn('python', ['-i']);
ps.stdout.pipe(process.stdout);
ps.stdin.write('1+1');
ps.stdin.end();

works a treat!



来源:https://stackoverflow.com/questions/10901660/node-js-interact-with-shell-application

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