【发布时间】:2015-02-27 11:57:12
【问题描述】:
我正在尝试编写一个小包装器来通过 nodejs 执行 shell 命令并将命令的输出打印到控制台。到目前为止,我已经完成了很多工作,但是控制台中的输出格式......很尴尬,而且我不知道为什么。 :(
#!/usr/bin/env node
var fs = require('fs');
var spawn = require('child_process').spawn;
MyWrapper( "ls", ["-l"], function(text) {
console.log(text)
fs.appendFileSync('testlog.txt', text)
});
function MyWrapper(cmd, args, callBack) {
var child = spawn(cmd, args)
var response = ""
child.stdout.on('data', function (buffer) { response += buffer.toString() });
child.stdout.on('end', function() { callBack (response) });
}
testlog.txt 完全没问题:
total 5
drwxrwxr-x+ 1 user user 114 Feb 26 23:09 test.git
-rw-rw-r--+ 1 user user 7524 Feb 27 12:45 testlog.txt
-rw-rw-r--+ 1 user user 524 Feb 27 01:17 test.txt
但是控制台输出是:
total 5
drwxrwxr-x+ 1 mmay mmay 114 Feb 26 23:09 test.git
-rw-rw-r--+ 1 git git 7750 Feb 27 12:47 testlog.txt
-rw-rw-r--+ 1 git git 524 Feb 27 01:17 test.txt
我不知道如何解决它......任何提示都会很棒:)
【问题讨论】:
-
你的意思是行前的空格?检查缓冲液。
-
感谢您的回复。缓冲区很好,小我很愚蠢:)
标签: node.js console console.log