如果有多个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}`);
});
If not encapsulated, you need to listen to multiple events, for example
cat /dev/urandom |od -x|tr -d ' '|head -n 1
You can also specify a pipe when spwan creates a child process, such as this
In actual environment, the information on stderr must be processed
The example you gave doesn’t have grep?
https://nodejs.org/api/child_...
Multiple options are passed as the second parameter in the form of an array: ls -lh /usr
The example you gave
cat /dev/urandom |od -x|tr -d ' '|head -n 1
Just follow the pipeline you screenshot and do it separately:
As for the pipeline, see how it is written in the example you screenshot, it is how the next command is executed in the callback