1

I am using child_process.exec to execute Ubuntu commands with node.js in coffeescript. When I execute the following commands:

list = child_process.exec("ls")
print list

It prints this:

[Object Object]

Why isn't a proper output of ls command printed? What should I do to get a proper output for commands?

2 Answers 2

2

You're attempting to run an asynchronous function synchronously. The correct way to do this is:

var exec = require('child_process').exec;

exec('ls', function (error, stdout, stderr) {
    console.log(stdout);
});

Source: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

If you really wish to execute a command synchronously, you can use execSync. However, I'd advise against that, since it blocks your node code from doing anything until the process finishes.

ExecSync: https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options

Sign up to request clarification or add additional context in comments.

1 Comment

It isn't working. Maybe the coffeescript is interfering with it. This is a part of the traceback: [Wed Jun 24 2015 17:13:40 GMT+0530 (IST)] ERROR Unable to load /home/cube26/Desktop/hubot 2.0/scripts/abuse: SyntaxError: unexpected { at Object.exports.throwSyntaxError (/home/cube26/Desktop/hubot 2.0/node_modules/hubot/node_modules/coffee-script/lib/coffee-script/helpers.js:197:13) at Object.parser.yy.parseError (/home/cube26/Desktop/hubot 2.0/node_modules/hubot/node_modules/coffee-script/lib/coffee-script/coffee-script.js:265:20)
0

Found it! Can be accessed using ->

print list.main.<attribute_name>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.