如何使用nodejs运行shell脚本文件?

pro*_*lic 28 shell cassandra node.js

我需要使用nodeJS运行一个shell脚本文件,该文件执行一组Cassandra DB命令.任何人都可以帮我这个

在db.sh文件中.

create keyspace dummy with   replication = {'class':'SimpleStrategy','replication_factor':3}

create table dummy (userhandle text, email text primary key , name text,profilepic)
Run Code Online (Sandbox Code Playgroud)

Sra*_*van 88

您可以使用nodejs的"子进程"模块在nodejs中执行任何shell命令或脚本.让我举个例子,我在nodejs中运行一个shell脚本(hi.sh).

hi.sh

echo "Hi There!"
Run Code Online (Sandbox Code Playgroud)

node_program.js

const exec = require('child_process').exec;
var yourscript = exec('sh hi.sh',
        (error, stdout, stderr) => {
            console.log(stdout);
            console.log(stderr);
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
        });
Run Code Online (Sandbox Code Playgroud)

在这里,当我运行nodejs文件时,它将执行shell文件,输出将是:

node node_program.js
Run Code Online (Sandbox Code Playgroud)

产量

Hi There!
Run Code Online (Sandbox Code Playgroud)

您只需在exec回调中提及shell命令或shell脚本即可执行任何脚本.

希望这可以帮助!快乐编码:)

  • 示例如下:https://github.com/rollup-umd/build/blob/master/src/index.js (2认同)

Mus*_*mun 59

您可以使用此模块https://www.npmjs.com/package/shelljs执行任何shell命令.

 const shell = require('shelljs')

 shell.exec('./path_to_your_file')
Run Code Online (Sandbox Code Playgroud)

  • 仔细研究答案.这是一个如何执行shell脚本文件的示例 (28认同)

Len*_*ean 11

你可以走了:

var cp = require('child_process');
Run Code Online (Sandbox Code Playgroud)

进而:

cp.exec('./myScript.sh', function(err, stdout, stderr) {
  // handle err, stdout, stderr
});
Run Code Online (Sandbox Code Playgroud)

在 $SHELL 中运行命令。
或者去

cp.exec('./myScript.sh', function(err, stdout, stderr) {
  // handle err, stdout, stderr
});
Run Code Online (Sandbox Code Playgroud)

无需 shell 即可运行文件。
或者去

cp.spawn('./myScript.sh', [args], function(err, stdout, stderr) {
  // handle err, stdout, stderr
});
Run Code Online (Sandbox Code Playgroud)

它与 cp.exec() 相同,但不会在 $PATH 中查找。

你也可以去

cp.execFile();
Run Code Online (Sandbox Code Playgroud)

使用 node.js 运行 javascript 文件,但在子进程中(对于大型程序)。

编辑

您可能还必须使用事件侦听器访问 stdin 和 stdout。例如:

cp.fork('myJS.js', function(err, stdout, stderr) {
  // handle err, stdout, stderr
});
Run Code Online (Sandbox Code Playgroud)


Ali*_*aji 9

另外,您可以使用shelljs插件。这很简单,而且是跨平台的。

安装命令:

npm install [-g] shelljs
Run Code Online (Sandbox Code Playgroud)

shellJS是什么

ShellJS 是基于 Node.js API 的 Unix shell 命令的可移植 (Windows/Linux/OS X) 实现。您可以使用它来消除 shell 脚本对 Unix 的依赖,同时仍然保留其熟悉且强大的命令。您还可以全局安装它,这样您就可以从 Node 项目外部运行它 - 告别那些粗糙的 Bash 脚本!

其工作原理的示例:

var shell = require('shelljs');

if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');
  shell.exit(1);
}

// Copy files to release dir
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');

// Replace macros in each .js file
shell.cd('lib');
shell.ls('*.js').forEach(function (file) {
  shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
  shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
});
shell.cd('..');

// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
  shell.echo('Error: Git commit failed');
  shell.exit(1);
}
Run Code Online (Sandbox Code Playgroud)

另外,您可以从命令行使用:

$ shx mkdir -p foo
$ shx touch foo/bar.txt
$ shx rm -rf foo
Run Code Online (Sandbox Code Playgroud)