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

在一个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) 

我也可以运行下面的脚本,而不是返回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) 

不过,我希望在同步调用中接收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) 

相关的软件版本是:

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

任何帮助赞赏谢谢。

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

除非另有说明,否则同步执行给定的命令。 […]返回forms为{code:…,stdout:…,stderr:…})的对象。

因此

 const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })