Nodejs下引入模块child_process实现调用shell
命令:npm install child_process --save
Nodejs中通过 exec执行shell脚本,并打印查询到的信息
var child = require('child_process');
child.exec('ls', function(err, sto) {
console.log(sto);//sto才是真正的输出,要不要打印到控制台,由你自己啊
})
Nodejs中通过exec执行shell脚本
let id=1;
const exec = require('child_process').execSync
app.post("/api/addorginfo", function (req, res) {
console.info('模拟用户调用shell脚本')
//自增值
var username=req.body.username;
var channelname=username+'channel'
id++
// 执行,test.sh脚本
exec('bash test.sh '+id+'' )
})
test.sh脚本,我们通过写入123文本里面
#!/bin/bash
# This is our first script.
echo "$1" > 123
输出信息vim 123