Running a shell command from Node.js without buffering output

前端 未结 4 687
傲寒
傲寒 2021-01-30 10:37

I\'m trying to launch a shell command from Node.js, without redirecting that command\'s input and output -- just like shelling out to a command using a shell script, or

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 10:47

    I haven't used it, but I've seen this library: https://github.com/polotek/procstreams

    It you'd do this. The .out() automatically pipes to the process's stdin/out.

    var $p = require('procstreams');
    $p('cat lines.txt').pipe('wc -l').out();
    

    If doesn't support shell syntax, but that's pretty trivial I think.

    var command_str = "cat lines.txt | wc -l";
    var cmds = command_str.split(/\s?\|\s?/);
    var cmd = $p(cmds.shift());
    while(cmds.length) cmd = cmd.pipe(cmds.shift());
    cmd
      .out()
      .on('exit', function() {
        // Do whatever
      });
    

提交回复
热议问题