42

I need to execute a bash script in node.js. Basically, the script will create user account on the system. I came across this example which gives me an idea how to go about it. However, the script itself needs arguments like the username, the password and the real name of the user. I still can't figure out how to pass those arguments to the script doing something like this:

var commands = data.toString().split('\n').join(' && ');

Does anyone have an idea how I can pass those arguments and execute the bash script within node.js over an ssh connection. thanks

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
joque
  • 813
  • 2
  • 9
  • 14

3 Answers3

68

See the documentation here. It is very specific on how to pass command line arguments. Note that you can use exec or spawn. spawn has a specific argument for command line arguments, while with exec you would just pass the arguments as part of the command string to execute.

Directly from the documentation, with explanation comments inline

var util  = require('util'),
    spawn = require('child_process').spawn,
    ls    = spawn('ls', ['-lh', '/usr']); // the second arg is the command 
                                          // options

ls.stdout.on('data', function (data) {    // register one or more handlers
  console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});

Whereas with exec

var util = require('util'),
    exec = require('child_process').exec,
    child;

child = exec('cat *.js bad_file | wc -l', // command line argument directly in string
  function (error, stdout, stderr) {      // one easy function to capture data/errors
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

Finally, note that exec buffers the output. If you want to stream output back to a client, you should use spawn.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • thanks for your reply. What I meant actually is that the command itself is in a bash script which requires arguments. And I don't know how to pass these arguments with node.js while reading the content of the bash file. – joque Sep 19 '11 at 07:10
  • 1
    @joque i don't know what you mean. If you were invoking the script on the CLI, you would do `command arg1 arg2...` you can do that with node, as shown in the examples. – hvgotcodes Sep 19 '11 at 12:50
  • well @hvgotcodes, I would like to follow the procedure in [http://blog.nodejitsu.com/nodejs-cloud-server-in-three-minutes] and execute the script **passing arguments**. Right now, I don't know how to do it, unless I break it down command after command. Do you see what I mean? – joque Sep 19 '11 at 14:59
  • 2
    I'm just curious, why do you require `util`? – KJ Price Jan 05 '16 at 02:48
  • @hvgotcodes how can i use spawn for 'sudo hcitool lescan --duplicates &' – ozata Aug 12 '16 at 21:25
11
var exec = require('child_process').exec;

var child = exec('cat *.js | wc -l', function(error, stdout, stderr) {
  if (error) console.log(error);
  process.stdout.write(stdout);
  process.stderr.write(stderr);
});

This way is nicer because console.log will print blank lines.

Anon
  • 117
  • 1
  • 3
10

You can use process.argv. It's an array containing the command line arguments. The first element will be node the second element will be the name of the JavaScript file. All next elements will be any additional command line you given.

You can use it like:

var username = process.argv[2];
var password = process.argv[3];
var realname = process.argv[4];

Or iterate over the array. Look at the example: http://nodejs.org/docs/latest/api/all.html#process.argv

zzeroo
  • 5,466
  • 4
  • 33
  • 49