亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

node.js - node的child_process.spawn(...[, options])怎么寫多個options?

瀏覽:113日期:2024-07-28 16:02:48

問題描述

node.js - node的child_process.spawn(...[, options])怎么寫多個options?

如果有多個grep,怎么寫到上面的語句中?例如cat /dev/urandom |od -x|tr -d ’ ’|head -n 1

在網(wǎng)上找了下,發(fā)現(xiàn)用以下的方法也行,使用spawn和exec有什么區(qū)別呢?

const exec = require(’child_process’).exec;exec(’cat /dev/urandom |od -x|tr -d ’ ’|head -n 1’, (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`);});

問題解答

回答1:

如果不封裝的話,你需要監(jiān)聽多個事件,舉例說cat /dev/urandom |od -x|tr -d ’ ’|head -n 1

const spawn = require(’child_process’).spawn;const cat = spawn(’cat’, [’/dev/urandom’]);const od = spawn(’od’,[’-x’]);const tr = spawn(’tr’,[’-d’,' ']);const head = spawn(’head’, [’-n’,1]);cat.stdout.on(’data’, data => od.stdin.write(data));cat.on(’close’, (code) => od.stdin.end());od.stdout.on(’data’, data => tr.stdin.write(data));od.on(’close’, (code) => tr.stdin.end());tr.stdout.on(’data’, data => head.stdin.write(data));tr.on(’close’, (code) => head.stdin.end());head.stdout.on(’data’, data => console.log(`${data}`));head.stdin.on(’error’,err=>head.stdin.end());

也可以在spwan創(chuàng)建子進(jìn)程的時候制定一下pipe管道,比如這樣

const spawn = require(’child_process’).spawn;const cat = spawn(’cat’, [’/dev/urandom’], {stdio: ’pipe’});const od = spawn(’od’,[’-x’], {stdio: [cat.stdout, ’pipe’, ’pipe’]});const tr = spawn(’tr’,[’-d’,’ ’], {stdio: [od.stdout, ’pipe’, ’pipe’]});const head = spawn(’head’, [’-n’,1], {stdio: [tr.stdout, ’pipe’, ’pipe’]});head.stdout.on(’data’, data => console.log(`${data}`));head.stdin.on(’error’,err=>head.stdin.end());

實(shí)際環(huán)境下,還要處理stderr那邊的信息

回答2:

你給的例子沒有g(shù)rep呀?

https://nodejs.org/api/child_...

多個options 以數(shù)組的形式作為 第二個參數(shù)傳遞: ls -lh /usr

const spawn = require(’child_process’).spawn;const ls = spawn(’ls’, [’-lh’, ’/usr’]);

你給的例子cat /dev/urandom |od -x|tr -d ’ ’|head -n 1

就照著你截圖的那個管道分開做:

// cat /dev/urandomconst cat = spawn(’cat’,[’/dev/urandom’]);//od -xconst od = spawn(’od’,[’-x’]);//tr -d ’ ’const tr = spawn(’tr’, [’-d’,'’ ’']);//head -n 1const head = spwan(’head’, [’-n’,’1’]);

然后管道的話看你截圖那個例子怎么寫的咯,就是回調(diào)里執(zhí)行下一個指令的樣子

主站蜘蛛池模板: 精品国产三级a∨在线观看 精品国产三级a在线观看 | a毛片久久免费观看 | 久久影视一区 | 美女叉开腿让男人捅 | 国产一区二区在线免费观看 | 日本久操 | 国产精品久久不卡日韩美女 | 久草在线首页 | 福利社在线视频 | 成人欧美在线视频 | 精品一区二区视频 | 一区二区三区视频在线 | 一级淫 | 亚洲情a成黄在线观看 | 不卡精品国产_亚洲人成在线 | 中文字幕一级毛片 | 欧美aaa毛片免费看 欧美aaa视频 | 免费在线一区二区三区 | 国产在线播放一区 | 性久久久久久 | 一级片成人 | 日本一级毛片免费 | 国产高清在线免费视频 | 久久九九精品一区二区 | 日韩美香港a一级毛片 | 欧美一级毛片免费播放器 | 亚洲va久久久噜噜噜久久狠狠 | 亚洲成在人线久久综合 | 美女视频黄色的免费 | 日韩精品一区二区三区乱码 | 91国内精品久久久久影院优播 | 欧美视频一区在线观看 | 国产手机在线小视频免费观看 | 日本一级毛片高清免费观看视频 | 91情侣高清精品国产 | 日韩一区二区三区精品 | 俄罗斯美女在线观看一区 | 欧美一级毛片免费高清aa | 九九成人 | 成人毛片免费观看视频在线 | 国产乱子伦在线观看不卡 |