av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

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

主站蜘蛛池模板: 国产美女一区 | 国产精品不卡视频 | 中文字幕精| 91精品国产乱码久久久久久久 | 日本久久久一区二区三区 | 高清一区二区视频 | 欧美αv| aaaaa毛片 | 久热精品在线观看视频 | 久久综合一区 | 日本一区二区在线视频 | 香蕉久久a毛片 | av黄色在线 | 中文字幕一区二区三区日韩精品 | 久热伊人| 成人一级片在线观看 | 在线成人免费av | 日日天天| 欧美日韩久久精品 | 免费国产黄网站在线观看视频 | 欧美高清免费 | 资源首页二三区 | 2020亚洲天堂| 国产精品成人国产乱一区 | 亚洲视频一区在线观看 | 中文字幕 在线观看 | 久久久久网站 | 精品1区| 欧美一二三 | 亚洲一区 中文字幕 | 日本不卡免费新一二三区 | 9久9久9久女女女九九九一九 | 亚洲一区二区中文字幕在线观看 | 五月天天丁香婷婷在线中 | 一区二区三区国产精品 | 久久亚洲高清 | 久久免费视频1 | 韩国毛片视频 | 九热在线 | 午夜视频在线免费观看 | 嫩草视频免费 |