如何在同步模式下运行节点shelljs并获取stdout和stderr

CPP*_*CPP 6 node.js shelljs

在nodejs脚本中,我有以下代码同步调用并从我调用的shell脚本返回stdout:

var sh = require('shelljs');

... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).stdout;
console.log(output);
... some more code (that shouldnt run until the script is complete)
Run Code Online (Sandbox Code Playgroud)

我还可以运行以下脚本,它将返回stderr:

var sh = require('shelljs');

... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).stderr;
console.log(output);
... some more code (that shouldnt run until the script is complete)
Run Code Online (Sandbox Code Playgroud)

但是我希望在同步调用中同时接收stdout和stderr.它可能是我在这里失踪的非常明显但我无法解决的问题.

我认为你曾经能够在以前的版本中运行以下命令,但这只是现在返回undefined:

var sh = require('shelljs');

... some code
var output = sh.exec('./someshellscript.sh', {silent:true}).output;
console.log(output);
... some more code (that shouldnt run until the script is complete)
Run Code Online (Sandbox Code Playgroud)

相关软件版本为:

  • Ubuntu:14.04.3 LTS
  • 节点:4.4.4
  • npm:2.15.1
  • shelljs:0.7.0

任何帮助表示感谢.

Mau*_*ppe 9

从README方法exec(command [, options] [, callback])

除非另有说明,否则同步执行给定的命令.[...],返回{code:...,stdout:...,stderr:...}形式的对象.

因此

const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })
Run Code Online (Sandbox Code Playgroud)