简体   繁体   English

使用node.js通过bash管道shell脚本

[英]Piping shell script through bash using node.js

Say I have this: 说我有这个:

const cp = require('child_process');

fs.createReadStream(__dirname + '/dummy.sh')
  .pipe(cp.spawn('bash').stdin).on('data', d => {
  console.log('data:', d);
});

and dummy.sh constains just echo "this is dummy" , for whatever reason no data comes through in the on data callback. 和dummy.sh constains只是echo "this is dummy" ,无论出于什么原因,数据回调中没有数据通过。 Does anyone know why that might be? 有谁知道为什么会这样? I would expect this output data: this is dummy 我希望这个输出data: this is dummy

You are not listening for the output of your bash command. 您没有在侦听bash命令的输出。
The on('data') event handler doesn't trigger your bash script output. on('data')事件处理程序不会触发bash脚本输出。

Instead, you should listen to the process stdout : 相反,你应该听过程stdout

const fs = require('fs');
const cp = require('child_process');

let cmd = cp.spawn('bash');
fs.createReadStream(__dirname + '/dummy.sh')
    .pipe(cmd.stdin).pipe(cmd.stdout).on('data', d => {
  console.log('data:', d.toString());  // data: this is dummy
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM