通过 ShellJS 返回值
Returning a value trough ShellJS
对于我的项目,我需要一个令牌的特定值,我通过 shell JS 执行 shell 文件获得该令牌。令牌在 shellJS 的输出中。
我的问题是我无法 return 我在函数中的值在别处使用令牌。
这是我的代码:
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,同步执行时(默认),returns一个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));
}
对于我的项目,我需要一个令牌的特定值,我通过 shell JS 执行 shell 文件获得该令牌。令牌在 shellJS 的输出中。
我的问题是我无法 return 我在函数中的值在别处使用令牌。
这是我的代码:
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,同步执行时(默认),returns一个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));
}