2

I want to run a bash script on my server using a Node js function and get the result back in Node through a callback. Sample script is as follows -

grep -c "eventName" 111data.csv

Is this possible? I looked at the following module but looks very complex. Does anyone know of a way this can be done?

https://www.npmjs.com/package/bashjs

2

1 Answer 1

3

You can execute a command

const { exec } = require('child_process');

const grep = exec('grep -c "eventName" 111data.csv', function (error, stdout, stderr) {
  if (error) {
    console.log(error.stack);
    console.log('Error code: '+error.code);
    console.log('Signal received: '+error.signal);
  }
  console.log('Child Process STDOUT: '+stdout);
  console.log('Child Process STDERR: '+stderr);
});

grep.on('exit', function (code) {
  console.log('Child process exited with exit code '+code);
});

Just modified the example from the Node.js article. Didn't actually test it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.