节点。js Shell脚本和参数

时间:2021-08-11 21:59:51

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:

我需要在node.js中执行一个bash脚本。基本上,脚本将在系统上创建用户帐户。我遇到了这个例子,这个例子让我知道该怎么做。但是,脚本本身需要参数,比如用户名、密码和用户的真实姓名。我仍然不知道如何将这些参数传递给脚本,做这样的事情:

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

是否有人知道如何在节点内传递这些参数并执行bash脚本。在ssh连接上的js。谢谢

3 个解决方案

#1


67  

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.

在这里看到的文档。如何传递命令行参数是非常具体的。注意,您可以使用exec或spawn。spawn对于命令行参数有一个特定的参数,而对于exec,您只需将参数作为命令字符串的一部分进行传递。

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.

最后,注意exec缓冲输出。如果希望将输出流回客户端,应该使用spawn。

#2


10  

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.

这种方式更好,因为控制台。日志将打印空行。

#3


8  

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.

您可以使用process.argv。它是一个包含命令行参数的数组。第一个元素是node,第二个元素是JavaScript文件的名称。所有下一个元素都将是您所提供的任何附加命令行。

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

或者遍历数组。请看示例:http://nodejs.org/docs/latest/api/all.html#process.argv

#1


67  

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.

在这里看到的文档。如何传递命令行参数是非常具体的。注意,您可以使用exec或spawn。spawn对于命令行参数有一个特定的参数,而对于exec,您只需将参数作为命令字符串的一部分进行传递。

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.

最后,注意exec缓冲输出。如果希望将输出流回客户端,应该使用spawn。

#2


10  

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.

这种方式更好,因为控制台。日志将打印空行。

#3


8  

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.

您可以使用process.argv。它是一个包含命令行参数的数组。第一个元素是node,第二个元素是JavaScript文件的名称。所有下一个元素都将是您所提供的任何附加命令行。

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

或者遍历数组。请看示例:http://nodejs.org/docs/latest/api/all.html#process.argv