我正在尝试实现我在这里找到的 shell 命令执行:node.js shell command execution
所以我试图让第二个答案(Chris Eineke)工作。但现在我没有输出。
这是我的尝试:
run_cmd = (cmd, args, done) ->
spawn = require('child_process').spawn
child = spawn(cmd, args)
result = { stdout: '' };
child.stdout.on \data ! (buffer) ->
result.stdout += buffer
child.stdout.on \end !-> done()
result
dir = run_cmd(
'ls', ['-a']
, !-> console.log('done')
)
console.log dir.stdout
它编译为:
run_cmd = function(cmd, args, done){
var spawn, child, result;
spawn = require('child_process').spawn;
child = spawn(cmd, args);
result = {
stdout: ''
};
child.stdout.on('data', function(buffer){
result.stdout += buffer;
});
child.stdout.on('end', function(){
done();
});
return result;
};
dir = run_cmd('ls', ['-a'], function(){
console.log('done');
});
console.log(dir.stdout);
但我看不到ls
结果。我的错误在哪里?或者我做错了什么?