child_process.spawn 乱码?
发布于 6 年前 作者 atian25 8884 次浏览 最后一次编辑是 2 年前
var ls= require('child_process').spawn('ping', ['www.baidu.com']);
ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

enter image description here

18 回复

中文Windows控制台是GBK编码的,应该需要转换为UTF-8: https://github.com/ashtuchkin/iconv-lite

纯Js呐,这是个好东西

@leizongmin yes,编码转换so easy

  var buffer = new Buffer(data);
  var str = iconv.decode(buffer, 'gbk');

为什么没有 ls.stdout..on 'end'

@italkboy

E:\Workspace\TZ\LTE-Test\client>npm install iconv
npm http GET http://registry.npmjs.vitecho.com/iconv
npm http 304 http://registry.npmjs.vitecho.com/iconv

> iconv@1.2.3 install E:\Workspace\TZ\LTE-Test\client\node_modules\iconv
> node-gyp rebuild


E:\Workspace\TZ\LTE-Test\client\node_modules\iconv>node "C:\Program Files\nodejs
\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js"
 rebuild
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.InvalidPlatform
.Targets(23,7): error MSB8007: The Platform for project 'iconv.vcxproj' is inva
lid.  Platform='x64'. You may be seeing this message because you are trying to
build a project without a solution file, and have specified a non-default Platf
orm that doesn't exist for this project. [E:\Workspace\TZ\LTE-Test\client\node_
modules\iconv\build\iconv.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\
npm\node_modules\node-gyp\lib\build.js:215:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:91:17)
gyp ERR! stack     at Process._handle.onexit (child_process.js:674:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modu
les\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\Workspace\TZ\LTE-Test\client\node_modules\iconv
gyp ERR! node -v v0.8.7
gyp ERR! node-gyp -v v0.6.5
gyp ERR! not ok
npm ERR! iconv@1.2.3 install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the iconv@1.2.3 install script.
npm ERR! This is most likely a problem with the iconv package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls iconv
npm ERR! There is likely additional logging output above.

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "iconv"
npm ERR! cwd E:\Workspace\TZ\LTE-Test\client
npm ERR! node -v v0.8.7
npm ERR! npm -v 1.1.49
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     E:\Workspace\TZ\LTE-Test\client\npm-debug.log
npm ERR! not ok code 0

非关键代码,就没贴上来

@atian25 npm install iconv-lite

@leizongmin —___----!! 看漏, 多谢

@leizongmin @italkboy

PS: 如何判断终端的编码呢? 譬如这段程序可能需要在linux和win下都运行, 如何兼容?

最终的实现:

"use strict";

var spawn = require('child_process').spawn;
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');

exports.ping = function(address, count, callback){

  var ping = spawn('ping', [address,'-n',count||5]);
  var bufferHelper = new BufferHelper();

  ping.stdout.on('data', function(chunk){
    bufferHelper.concat(chunk);
  });

  ping.stdout.on('end',function(){
    var str = iconv.decode(bufferHelper.toBuffer(),'gbk');
    callback(null,str);
  }); 
}

不知道怎么获取终端的编码。貌似Linux一般都是UTF-8的,只有Windows才是ANSI编码,而且若终端输出的是英文字符,用哪个编码方式都是可以正常显示的。 如果是这样的话,简单地判断一下操作系统类型吧

@leizongmin 也只能不严谨了. 本想判断操作系统类型, 终端编码, 和终端的语言的. 那「终端的语言」这个怎么判断呢? 或者能否指定终端的lang? 中文的输出和英文的输出,字符串分析是不一样的。 (win肯定是中文的了,linux就怕有些是e文的),暂时想到的办法是正则式判断是否存在中文。

@leizongmin 当然。。。有点离题了,这好像属于cmd和shell的环境变量问题。

@atian25

  var str = iconv.decode(data, 'gbk');
  console.log(str == data);

=w= 这样可以判断是否是gbk,当然这方法有点2,要每种编码都遍历判断一遍

@atian25 不过iconv-lite效率还不错

@italkboy PHP中有个自动判断字符串编码的函数,若iconv-lite有的话就更方便了

怎么样只取命令输出的前面几行,比如1行或者2

6飞 大佬们6年前就遇到了这个问题,小弟佩服啊

回到顶部