Mchoeti
Mchoeti

Reputation: 536

Execute Shell Script in JavaScript IF Clause

Hi everybody a simple question but i do not find the answer for it. I want execute a shell file when the condition in my if clause is true I tried it with that sniplet:

if ( state === true) 
{
   console.log("Hello");
   sudo ./test.sh
}

Could someone please explain how to solve it in a simple way?

Upvotes: 0

Views: 1993

Answers (1)

Rakesh Gupta
Rakesh Gupta

Reputation: 3750

From what I understood from the question, I think you are trying to write server-side JavaScript code to execute a shell script.

As @epascarello mentioned, you can use NodeJS exec() function as under:

exec('sh sudo ./test.sh', function(error, stdout, stderr){
      console.log(error, stdout, stderr);
})

For more information please refer to this thread: Run shell script with node.js (childProcess)

Upvotes: 1

Related Questions