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

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:77日期: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

在網上找了下,發現用以下的方法也行,使用spawn和exec有什么區別呢?

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:

如果不封裝的話,你需要監聽多個事件,舉例說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創建子進程的時候制定一下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());

實際環境下,還要處理stderr那邊的信息

回答2:

你給的例子沒有grep呀?

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

多個options 以數組的形式作為 第二個參數傳遞: 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’]);

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

主站蜘蛛池模板: 精品欧美高清不卡在线 | 亚洲免费在线观看视频 | 黄a视频在线观看 | 最新国产精品亚洲二区 | 成年人视频网站免费 | 一区二区三区四区在线免费观看 | 青青草国产一区二区三区 | 99久久国产综合精品1尤物 | 中文字幕va一区二区三区 | 国内成人精品亚洲日本语音 | 一级待一黄aaa大片在线还看 | 香港全黄一级毛片在线播放 | 国产高清片 | 男女男精品视频在线播放 | 二区三区在线 | 台湾黄三级高清在线观看播放 | 男女性高清爱潮视频免费观看 | 日本国产在线 | 毛片啪啪视频 | 日韩欧一级毛片在线播无遮挡 | 日韩精品视频免费在线观看 | 日本精品一区二区三区在线视频 | 亚洲区免费 | 一级做a爱片久久毛片 | 免费看a视频| 国产成人丝袜视频在线视频 | 成人影院vs一区二区 | 欧美久久视频 | 国产乱肥老妇精品视频 | 欧洲一级片 | 久草资源网 | 国产一区二区亚洲精品 | 欧洲做人爱c欧美 | 亚洲欧美二区三区久本道 | 国产午夜永久福利视频在线观看 | 亚洲图片偷拍自拍 | 国内精品久久久久不卡 | 国产婷婷一区二区三区 | 黄色在线视频网 | 久久亚洲在线 | 久操精品在线 |