I'm trying to execute a command line command(here: cat) from nodejs using the spawn command.
var spawn = require('child_process').spawn;
var cat = spawn('cat');
result = new Buffer(2048);
cat.stdout.on('data', function (data)
{
data.copy(result);
});
cat.on('exit', function (code)
{
console.log(result.toString());
});
cat.stdin.write(InBUFFER);
cat.stdin.end();
The output of the command seems to be somewhere in the Buffer, but the rest is outputted as well. Is there some kind of null character that I could use to determine when the output ends?
Update: To clarify things: There's a lot of random characters in the ouput and they seem to be part of the Buffer. I'm looking for a way to divide the useful output of the command line command from the rest of the buffer.
This depends on what you have inside InBUFFER, which you're not showing.
If you have a string in InBUFFER
, for example "test"
, the result
will contain test
followed by a lot of zeros, up to 2048 (the 0s will get converted to blank when toString
is called).
If you have garbage inside InBUFFER
, you'll get garbage as output. Maybe you're reading a file and not setting the character encoding properly.