簡體   English   中英

如何從 Node.js 運行 bash 命令並獲得結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM