亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:75日期: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’]);

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

主站蜘蛛池模板: 黄色一级片免费在线观看 | 青青草国产一区二区三区 | 永久免费不卡在线观看黄网站 | 日本一级网站 | 国产九九视频在线观看 | 韩国成人毛片aaa黄 韩国成人在线视频 | 欧美精品国产一区二区三区 | 男女爱爱激情视频在线观看 | 视频二区国产 | 亚洲欧美日韩一区二区 | 成年人在线观看视频网站 | 日韩精品欧美一区二区三区 | 欧美成人免费全部观看天天性色 | 2021久久精品99精品久久 | 一级一级特黄女人精品毛片 | 黄色成年人 | 噜噜噜噜精品视频在线观看 | 999国产一区二区三区四区 | 夜婷婷| 在线精品福利视频你懂的 | 欧美3p在线观看一区二区三区 | 亚洲无线码1区 | 亚洲图区综合网 | 国产一区三区二区中文在线 | 十级毛片 | 欧美成a人片免费看久久 | 日韩中文字幕久久精品 | 亚洲欧美综合 | 欧美麻豆久久久久久中文 | 国产精品尹人在线观看免费 | 亚洲视频区 | 欧美一级毛片生活片 | 精品国产福利在线 | 成年人啪啪网站 | 台湾一级毛片免费播放 | 国产精品视频福利 | 久草水蜜桃 | 久久国产一级毛片一区二区 | 亚洲七七久久综合桃花 | 欧美高清在线精品一区二区不卡 | 成人在线视频网站 |