Node.js:execSync执行一个shell命令

默认输出是Buffer对象

const {
    
     execSync } = require('child_process')

let out = execSync("echo 'hi'")
console.log(out);
// <Buffer 68 69 0a>

需要转为字符串

const {
    
     execSync } = require('child_process')

let out = execSync("echo 'hi'")
console.log(out.toString());
// hi

参考文章

  1. https://www.runoob.com/nodejs/nodejs-process.html
  2. https://blog.csdn.net/weixin_43972437/article/details/130643741

猜你喜欢

转载自blog.csdn.net/mouday/article/details/131940726