【问题标题】:How to run shell script file using nodejs?如何使用 nodejs 运行 shell 脚本文件?
【发布时间】:2017-11-22 16:27:56
【问题描述】:

我需要使用 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)

【问题讨论】:

    标签: node.js shell cassandra


    【解决方案1】:

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

    hi.sh

    echo "Hi There!"
    

    node_program.js

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

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

    运行

    node node_program.js
    

    输出

    Hi There!
    

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

    希望这会有所帮助!快乐编码:)

    【讨论】:

    【解决方案2】:

    您可以使用 shelljs module 执行任何 shell 命令

     const shell = require('shelljs')
    
     shell.exec('./path_to_your_file')
    

    【讨论】:

    • 这不是目的。我需要运行本地系统中存在的脚本文件。该模块用于执行命令。你能告诉我如何运行一个shell脚本文件吗?
    • 仔细查看答案。这是一个如何执行 shell 脚本文件的示例
    • 如答案中所述,最好使用 shrlljs 运行 bash 脚本,而不是一一运行命令。
    【解决方案3】:

    你可以去:

    var cp = require('child_process');
    

    然后:

    cp.exec('./myScript.sh', function(err, stdout, stderr) {
      // handle err, stdout, stderr
    });
    

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

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

    在没有外壳的情况下运行文件。
    或者去

    cp.execFile();
    

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

    你也可以去

    cp.fork('myJS.js', function(err, stdout, stderr) {
      // handle err, stdout, stderr
    });
    

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

    编辑

    您可能还必须使用事件侦听器访问标准输入和标准输出。例如:

    var child = cp.spawn('./myScript.sh', [args]);
    child.stdout.on('data', function(data) {
      // handle stdout as `data`
    });
    

    【讨论】:

    • 这行不通。您不能要求 Bash 脚本,只能要求节点模块。
    • @HarryCramer 我不是。
    【解决方案4】:

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

    安装命令:

    npm install [-g] shelljs
    

    什么是shellJS

    ShellJS 是 Unix 的可移植 (Windows/Linux/OS X) 实现 基于 Node.js API 的 shell 命令。你可以用它来消除 你的 shell 脚本对 Unix 的依赖,同时仍然保持它的 熟悉而强大的命令。您也可以全局安装它 您可以从外部节点项目运行它 - 告别那些 粗糙的 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);
    }
    

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

    $ shx mkdir -p foo
    $ shx touch foo/bar.txt
    $ shx rm -rf foo
    

    【讨论】:

      【解决方案5】:

      #!/usr/bin/env 节点

      console.log("你好世界!") // 把你的代码放在这里。

      执行这个文件:

      ./app.js

      文件app.js应该有可执行权限

      chmod u+x app.js

      【讨论】:

      • 他们的意思是运行一个shell文件,而不是一个js文件
      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2011-02-22
      • 2014-10-10
      • 2015-08-11
      相关资源
      最近更新 更多