NodeJS读取并解析每行stdout

Pro*_*mer 5 javascript node.js

我有一个NodeJS脚本'exec是一个子进程来捕获文件的cat转储:

var exec = require('child_process').exec;
var result = '';
var child = exec('./scripts/first.sh',function(err, stdout, stderr) {
    result = stdout.split("=");
});
Run Code Online (Sandbox Code Playgroud)

如果文件不存在,我会转储不同的文件:

var result = '';
var child = exec('./scripts/first.sh',function(err, stdout, stderr) {
    result = stdout.split("=");
    if(stdout.indexOf('No such file or directory') != -1){
        var child = exec('./scripts/second.sh', function(err, stdout, stderr) {
            result = stdout.split("=");
    });
});
Run Code Online (Sandbox Code Playgroud)

最后我记录了结果变量的值:

console.log(result);
Run Code Online (Sandbox Code Playgroud)

这些文件将包含如下所述的数据:

line_1 = 7888
line_2 = 8998
line_3 = 9090
line_4 = 9097
Run Code Online (Sandbox Code Playgroud)

我需要解析并提取line_1和line_3的值?

结果变量不显示任何值.我的想法是将stdout数据放在字符串变量中并使用一些搜索机制.

虽然我不确定这种方法,因为我对JS/NodeJS没有多少经验.

== ==编辑

请找到我写的函数的副本.

var exec = require('child_process').exec;

function getdetail() {
        var result = '';
        var child = exec('./scripts/first.sh', function(err, stdout, stderr) {
                if(stdout.indexOf('No such file or directory') != -1){
                        var child = exec('./scripts/second.sh',function(err, stdout, stderr) {
                        result = stdout.toString().split("=");
                        console.log(result);
                        });
                }
                else
                {
                        result = stdout.toString().split("=");
                        console.log(result);
                }
        });
}
Run Code Online (Sandbox Code Playgroud)

stdout流对象上的tostring()完成了这个技巧,但我得到了如下所述的控制台日志:

[ 'spawn ssh -o UserKnownHostsFile',
  '/dev/null -o StrictHostKeyChecking',
  'no user@www.mybox.com cat ver.txt\r\nWarning: Permanently added \'www.mybox.com,XX.XX.XX.XX\' (RSA) to the list of known hosts.\r\r\nuser@www.mybox.com\'s password: \r\line_1',
  '9400\r\nline_2',
  '3508\r\nline_3',
  '77f3\r\nline_4',
  '/tmp\r\nline_5',
  '/tmp/ramdisk/\r\nline_5',
  '77f3\r\n' ]
Run Code Online (Sandbox Code Playgroud)

如何提取line_1和line_3的值?

jsa*_*nen 11

exec是异步的.因此,如果你写的东西如下:

var result = '';
var child = exec('...'), function() { result = 'abc'; } );
console.log(result);
Run Code Online (Sandbox Code Playgroud)

然后result可能是空的,因为console.log(result)在exec返回其回调并填写新值之前可以并且经常会执行.

要解决此问题,您需要在exec的回调函数中异步处理结果.

此外,我不确定您检查错误的方式是否最好.您可以简单地测试是否err具有非空值,而不是检查"没有这样的文件或目录" :

if(err) {
Run Code Online (Sandbox Code Playgroud)

把这一切放在一起我们最终得到以下代码:

var exec = require('child_process').exec;

var result = '';
var processResult = function(stdout) {
    var result = stdout.split("=");
    console.log(result);
};

var child = exec('./scripts/first.sh',function(err, stdout, stderr) {
    if(err) {
        var child = exec('./scripts/second.sh', function(err, stdout, stderr) {                         
            processResult(stdout);
        });
    } else {            
        processResult(stdout);
    }
});
Run Code Online (Sandbox Code Playgroud)

如果需要进一步处理stdout数据,则需要遍历它以找出包含"key = value"的所有可能出现的字符串.这是一个粗略的想法:

var processResult = function(stdout) {  
    var lines = stdout.toString().split('\n');
    var results = new Array();
    lines.forEach(function(line) {
        var parts = line.split('=');
        results[parts[0]] = parts[1];
    });

    console.log(results);
};
Run Code Online (Sandbox Code Playgroud)

我希望这能让你开始.