简体   繁体   中英

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. Does anyone know why that might be? I would expect this output data: this is dummy

You are not listening for the output of your bash command.
The on('data') event handler doesn't trigger your bash script output.

Instead, you should listen to the process 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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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