如何grep想要的文本并接收到stdout作为
var express = require('express');
var exec = require("child_process").exec;
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || xxxx;
app.get('/', function(req, res) {
res.json('API is Online!');
});
app.post('/data', function(req, res){
//executes my shell script - test.sh when a request is posted to the server
exec('sh test.sh' , function (err, stdout, stderr) {
if (!err) {
res.json({'results': stdout})
}
});
})
app.listen(port);
console.log('Listening on port ' + port);
这是在bash中运行的代码
#!/bin/bash
free -m
https://stackoverflow.com/users/2076949/darklightcode
我可以分割结果,但我能像这样进入数组吗
{ results : [ { "total" : the total number, "free" : the free number, "etc" : "etc" } ] }
不是这样的
{
"results": [
" total used free shared buff/cache available",
"Mem: 992 221 235 16 534 590",
"Swap: 263 245 18",
""
] }