11

nodejsスクリプト内に、呼び出しを同期的に行い、呼び出しているシェルスクリプトから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

どんな助けでも感謝します。

4

1 に答える 1

22

メソッドのREADMEからexec(command [, options] [, callback])

特に指定がない限り、指定されたコマンドを同期的に実行します。[...]、フォーム { code:..., stdout:... , stderr:... }) のオブジェクトを返します。

したがって

const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })
于 2016-05-16T22:29:30.550 に答える