Sush
Sush

Reputation: 1457

execute a shell script inside nodejs code

i want to execute following command inside node js code

diff <(git log 01) <(git log 02)

in command line it is working properly and gettig desired output i want

here is my node code

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)
      }
    }
  });

But while executing above code am getting'

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

Upvotes: 3

Views: 1067

Answers (2)

rts
rts

Reputation: 191

The command you want to exec uses a bash specific syntax for process substitution. I'm assuming you're using node's child_process module for your exec function. If that's the case then what you've written doesn't work because the child_process module is providing access to popen(3).

Pop into the man page for popen and you'll find that the command gets passed to /bin/sh which does not support the syntax you're using.

Upvotes: 1

gtramontina
gtramontina

Reputation: 1136

Try running it like this:

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);
});

Upvotes: 5

Related Questions