国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

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

問(wèn)題描述

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

如果有多個(gè)grep,怎么寫(xiě)到上面的語(yǔ)句中?例如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}`);});

問(wèn)題解答

回答1:

如果不封裝的話,你需要監(jiān)聽(tīng)多個(gè)事件,舉例說(shuō)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)程的時(shí)候制定一下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:

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

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

多個(gè)options 以數(shù)組的形式作為 第二個(gè)參數(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

就照著你截圖的那個(gè)管道分開(kāi)做:

// 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’]);

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

主站蜘蛛池模板: 久久院线 | 精品久久久久久久久中文字幕 | 欧美日韩在线视频播放 | 欧美一级片播放 | 成人看的午夜免费毛片 | 99国产精品久久久久久久日本 | 狠狠色丁香婷婷综合小时婷婷 | 欧美精品成人一区二区在线观看 | 亚洲天堂免费在线视频 | 九九久久精品国产 | 在线观看一级片 | 美女视频在线观看黄 | 色偷偷亚洲第一成人综合网址 | 91精品综合 | 亚洲成a v人片在线看片 | 伊人波多野结衣 | 目韩一区二区三区系列片丶 | 久久精品在现线观看免费15 | 国产免费怡红院视频 | 99视频免费 | 欧美人成在线观看ccc36 | 女人张开腿男人猛桶视频 | 成人看片免费 | 亚洲男女在线 | 欧美成年人网站 | 免费91最新地址永久入口 | 欧美亚洲免费 | 久久免费视频观看 | 亚洲一级二级三级 | 成人免费网站在线观看 | 亚洲一区二区三区一品精 | 国产不卡精品一区二区三区 | 玖草在线 | 日韩在线手机看片免费看 | 在线视频久久 | 国产主播福利片在线观看 | 欧美国一级毛片片aa | 国产精品成人aaaaa网站 | 日本亚洲视频 | 国产91久久久久久久免费 | 国产美女在线一区二区三区 |