0

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?

Samurai Jack
  • 2,985
  • 8
  • 35
  • 58
Mchoeti
  • 536
  • 1
  • 8
  • 24

1 Answers1

1

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)

Community
  • 1
  • 1
Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24
  • ok.. it is working when i am executing it via the console. but if i add this in my if clause the response from the console is an error command not found. – Mchoeti May 09 '17 at 14:30
  • when you say that it is working "via the console", do you mean the Unix shell or javascript console? Also, try using the absolute path to your shell script. – Rakesh Gupta May 09 '17 at 16:37
  • hoi, thanks i solved it and added the full patch.. thanks. the absolute patch was the solution – Mchoeti May 10 '17 at 17:37