Possible Duplicate:
Node.js Shell Script And Arguments
I'd like to get the output of a shell command as a string in node.js, but I'm not exactly sure where to start. An example of such a command is the bash command "ls", which lists the contents of the current folder, and prints the results in the terminal window. Is it possible to convert this output to a Javascript string?
See the documentation for "Child Processes" in the nodejs.org API documentation which provides example code to handle the exact task you mention, i.e. running the 'ls' command and capturing its output.
var spawn=require('child_process').spawn,
ls=spawn('ls', ['-lh', '/usr']); // runs the 'ls -lh /usr' shell cmd
ls.stdout.on('data', function(data) { // handler for output on STDOUT
console.log('stdout: '+data);
});
ls.stderr.on('data', function(data) { // handler for output on STDERR
console.log('stderr: '+data);
});
ls.on('exit', function(code) { // handler invoked when cmd completes
console.log('child process exited with code '+code);
});