Node.js Shell 脚本和参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7464036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:35:28  来源:igfitidea点击:

Node.js Shell Script And Arguments

bashnode.js

提问by joque

I need to execute a bash script in node.js. Basically, the script will create user account on the system. I came across this examplewhich 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

有谁知道我如何传递这些参数并通过 ssh 连接在 node.js 中执行 bash 脚本。谢谢

回答by hvgotcodes

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

请参阅此处的文档。关于如何传递命令行参数非常具体。请注意,您可以使用execspawnspawn具有用于命令行参数的特定参数,而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

而与 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.

回答by Anon

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.

这种方式更好,因为 console.log 将打印空行。

回答by zzeroo

You can use process.argv. It's an array containing the command line arguments. The first element will be nodethe 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