通过ShellJS返回值



对于我的项目,我需要通过shell JS执行shell文件而获得的令牌中的特定值。该令牌在shellJS的输出中。

我的问题是,我无法返回函数中的值来在其他地方使用令牌。

这是我的代码:

const shell = require('shelljs');

const bypass = () => {
let R2;
var put = shell.exec('sh ./scripts/script.sh', (err, stdout) => {
if (err) {
console.log(err)
} else {
let tableau = (stdout.split(' '));
let test = tableau[tableau.length - 1].split(':');
let test3 = (test[1]).split(',');
R2 = (test3[0].substr(1, test3[0].length - 2));
}
})
return R2
}
bypass();

我希望在执行如图所示的函数时能够获取R2值。

任何想法都将不胜感激!

谢谢。

致问候,

艾默里克。

shell.exec在同步执行时(默认值(会返回一个ShellString。

ShellString有.stdout.stderr.code

var put = shell.exec('sh ./scripts/script.sh');
if (put.stderr === ''){
let tableau = (put.stdout.split(' '));
let test = tableau[tableau.length - 1].split(':');
let test3 = (test[1]).split(',');
let R2 = (test3[0].substr(1, test3[0].length - 2));
}

最新更新