【问题标题】:Returning a value trough ShellJS通过 ShellJS 返回值
【发布时间】:2020-07-08 16:56:30
【问题描述】:

对于我的项目,我需要通过 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 值。

任何想法都将不胜感激!

谢谢。

最好的问候,

艾美式。

【问题讨论】:

    标签: javascript node.js shelljs


    【解决方案1】:

    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));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 2017-07-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2013-05-03
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多