在nodejs代码中执行shell脚本

Sus*_*ush 3 node.js

我想在节点js代码中执行以下命令

diff <(git log 01) <(git log 02)
Run Code Online (Sandbox Code Playgroud)

在命令行它正常工作和gettig所需的输出我想要的

这是我的节点代码

var command = "diff <(git log 01) <(git log 02)"
console.log(command)
  exec(command, function (error, stdout, stderr) {
    if (error !== null) {
      console.log(error)

    } else {

        console.log(stdout)
      }
    }
  });
Run Code Online (Sandbox Code Playgroud)

但是在执行上面的代码时我得到了'

diff <(git 01) <(git log 02)
{ [Error: Command failed: /bin/sh: 1: Syntax error: "(" unexpected
] killed: false, code: 2, signal: null }
Run Code Online (Sandbox Code Playgroud)

gtr*_*ina 5

尝试像这样运行:

var spawn = require('child_process').spawn;
var command = "diff <(git log 01) <(git log 02)";
console.log(command)

var diff = spawn('bash', ['-c', command]);
diff.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

diff.stderr.on('data', function (data) {
  console.error('stderr: ' + data);
});
Run Code Online (Sandbox Code Playgroud)