Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Shell Script And Arguments

Tags:

bash

node.js

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

like image 849
joque Avatar asked Sep 18 '11 19:09

joque


People also ask

What happens if you execute the node command without any arguments?

At the very least, a script that's run without any arguments will still contain two items in the array, the node executable and the script file that is being run.

What is argument in node JS?

The arguments object is an array-like object. It has a length property that corresponds to the number of arguments passed into the function. You can access these values by indexing into the array, e.g. arguments[0] is the first argument.

How do I run a shell script in node JS?

Node. js can run shell commands by using the standard child_process module. If we use the exec() function, our command will run and its output will be available to us in a callback. If we use the spawn() module, its output will be available via event listeners.


1 Answers

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.

like image 122
hvgotcodes Avatar answered Sep 22 '22 09:09

hvgotcodes