我必须在node.js中运行一些交互式shell命令.让我们的交互式shell $ python:
var cp = require('child_process');
var pythonChildProcess = cp.spawn('python');
pythonChildProcess.stdout.on("data", function(data) {
console.log('data successfully written!', data); // never outputs anything
});
pythonChildProcess.stdin.write('1 + 1');
pythonChildProcess.stdin.end();
Run Code Online (Sandbox Code Playgroud)
此代码不输出任何内容(但应该是stdout 2).
但如果它会,那将会有另一个问题:如何让它互动?我打电话过程结束pythonChildProcess.stdin.end();!但我只是想结束stdin并写下一个stdin!
UPD:如果我可以模仿按下enter按钮 - 我将能够以交互方式写入该过程.但是添加\n到输入字符串的末尾并没有帮助.