1

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?

0

1 Answer 1

3

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.