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

时间:2021-01-22 02:24:20

I need to run a shell script file using nodeJS that executes a set of Cassandra DB commands. Can anybody please help me on this

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

inside db.sh file.

在db.sh文件中。

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

create table dummy (userhandle text, email text primary key , name text,profilepic)

2 个解决方案

#1


20  

You can execute any shell command using this module https://www.npmjs.com/package/shelljs.

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

 const shell = require('shelljs');
 //shell.exec(comandToExecute, {silent:true}).stdout;
 //you need little improvisation
 shell.exec('./path_to_ur_file')

#2


38  

You could use "child process" module of nodejs to execute any shell commands or scripts with in nodejs. Let me show you with an example, I am running a shell script(hi.sh) with in nodejs.

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

hi.sh

hi.sh

echo "Hi There!"

node_program.js

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}`);
            }
        });

Here, when I run the nodejs file, it will execute the shell file and the output would be:

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

Run

node node_program.js

output

产量

Hi There!

You can execute any script just by mentioning the shell command or shell script in exec callback.

您可以通过在exec回调中提及shell命令或shell脚本来执行任何脚本。

Hope this helps! Happy coding :)

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

#1


20  

You can execute any shell command using this module https://www.npmjs.com/package/shelljs.

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

 const shell = require('shelljs');
 //shell.exec(comandToExecute, {silent:true}).stdout;
 //you need little improvisation
 shell.exec('./path_to_ur_file')

#2


38  

You could use "child process" module of nodejs to execute any shell commands or scripts with in nodejs. Let me show you with an example, I am running a shell script(hi.sh) with in nodejs.

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

hi.sh

hi.sh

echo "Hi There!"

node_program.js

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}`);
            }
        });

Here, when I run the nodejs file, it will execute the shell file and the output would be:

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

Run

node node_program.js

output

产量

Hi There!

You can execute any script just by mentioning the shell command or shell script in exec callback.

您可以通过在exec回调中提及shell命令或shell脚本来执行任何脚本。

Hope this helps! Happy coding :)

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