2016-05-16 16 views
3

在一個的NodeJS腳本我有以下的代碼,這使得呼叫同步,並返回從shell腳本我打電話的標準輸出:如何在同步模式下運行節點shelljs並獲得輸出和錯誤

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

任何幫助表示讚賞謝謝。

回答

2

README for the method exec(command [, options] [, callback])

執行給定的命令同步地,除非另有說明。 [...]返回表單{code:...,stdout:...,stderr:...})的對象。

因此

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

感謝這麼多的提示答案。完美:) – Craig

相關問題