[英]How to run bash commands from Node.js and get result
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.我想使用 Node js function 在我的服务器上运行 bash 脚本,并通过回调将结果返回到 Node 中。 Sample script is as follows -
示例脚本如下 -
grep -c "eventName" 111data.csv
grep -c "事件名称" 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
https://www.npmjs.com/package/bashjs
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 .刚刚修改了 Node.js 文章中的示例。 Didn't actually test it
并没有实际测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.