简体   繁体   中英

How to execute multiple shell commands using node.js?

Maybe I haven't groked the asynchronous paradigm yet, but I want to do something like this:

var exec, start;
exec = require('child_process').exec;
start = function() {
  return exec("wc -l file1 | cut -f1 -d' '", function(error, f1_length) {
    return exec("wc -l file2 | cut -f1 -d' '", function(error, f2_length) {
      return exec("wc -l file3 | cut -f1 -d' '", function(error, f3_length) {
        return do_something_with(f1_length, f2_length, f3_length);
      });
    });
  });
};

It seems a little weird to keep nesting those callbacks every time I want to add a new shell command. Isn't there a better way to do it?

As Xavi said you can use TwoStep. On the other hand I use Async JS library. Your code may look like this:

async.parallel([
    function(callback) {
        exec("wc -l file1 | cut -f1 -d' '", function(error, f1_length) {
            if (error)
                return callback(error);
            callback(null, f1_length);
        });
    },
    function(callback) {
        exec("wc -l file2 | cut -f1 -d' '", function(error, f2_length) {
            if (error)
                return callback(error);
            callback(null, f2_length);
        });
    },
    function(callback) {
        exec("wc -l file3 | cut -f1 -d' '", callback);
    }
],
function(error, results) {
    /* If there is no error, then
       results is an array [f1_length, f2_length, f3_length] */
    if (error)
        return console.log(error);
    do_something_with(results);
});

Async gives tons of other options. Read the documentation and try it out! Note that for f3_length I just used callback when calling exec . You can do this with other calls as well (so your code will be shorter). I just wanted to show you how it works.

I personally use TwoStep in these situations:

var TwoStep = require("two-step");
var exec, start;
exec = require('child_process').exec;
start = function() {
  TwoStep(
    function() {
      exec("wc -l file1 | cut -f1 -d' '", this.val("f1_length"));
      exec("wc -l file2 | cut -f1 -d' '", this.val("f2_length"));
      exec("wc -l file3 | cut -f1 -d' '", this.val("f3_length"));
    },
    function(err, f1_length, f2_length, f3_length) {
      do_something_with(f1_length, f2_length, f3_length);
    }
  );
};

That said, there are a ton of flow controll libraries out there. I encourage you to try some out: https://github.com/joyent/node/wiki/Modules#wiki-async-flow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM