![](/img/trans.png)
[英]Is there a way to run Node.js with Bash commands in Windows?
[英]How to run bash commands from Node.js and get result
我想使用 Node js function 在我的服务器上运行 bash 脚本,并通过回调将结果返回到 Node 中。 示例脚本如下 -
grep -c "事件名称" 111data.csv
这可能吗? 我查看了以下模块,但看起来非常复杂。 有谁知道可以做到这一点的方法?
https://www.npmjs.com/package/bashjs
你可以执行一个命令
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);
});
刚刚修改了 Node.js 文章中的示例。 并没有实际测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.