如何 运行 在同步模式下节点 shelljs 并获取 stdout 和 stderr
how to run node shelljs in sync mode and get stdout and stderr
在 nodejs 脚本中,我有以下代码同步调用,returns 来自我正在调用的 shell 脚本的标准输出:
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)
我还可以 运行 以下脚本代替 return 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。这可能是我在这里遗漏的很明显的东西,但我无法解决。
我认为您曾经能够 运行 在以前的版本中使用以下命令,但现在 return 未定义:
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
感谢任何帮助。
来自README for the method exec(command [, options] [, callback])
Executes the given command synchronously, unless otherwise specified. [...], returns an object of the form { code:..., stdout:... , stderr:... }).
因此
const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })
在 nodejs 脚本中,我有以下代码同步调用,returns 来自我正在调用的 shell 脚本的标准输出:
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)
我还可以 运行 以下脚本代替 return 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。这可能是我在这里遗漏的很明显的东西,但我无法解决。
我认为您曾经能够 运行 在以前的版本中使用以下命令,但现在 return 未定义:
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
感谢任何帮助。
来自README for the method exec(command [, options] [, callback])
Executes the given command synchronously, unless otherwise specified. [...], returns an object of the form { code:..., stdout:... , stderr:... }).
因此
const { stdout, stderr, code } = sh.exec('./someshellscript.sh', { silent: true })