Process Substitution - Node.js child_process

I am trying to start a child process to modify a file (in two steps) before reading the changed contents from stdout. I am trying to do this using a process override that works fine in bash, but not when I try to use it from node.

This is similar to what the team looks like.

var p = exec('command2 <(capture /dev/stdout | command1 -i file -) -', function (error, stdout, stderr) { console.log(stderr); }); 

stderr prints:

 /bin/sh: -c: line 0: syntax error near unexpected token `(' 

What is the correct way to do this in node?

+4
source share
3 answers

I solved this by putting the commands in a shell script and calling the script from the node child process. I also need to add the following to install bash in posix mode in order to allow process replacement:

 set +o posix 

Perhaps a more convenient way to do this directly from node, but he did the job. Hooray!

+3
source

You can spawn a child_process with replacing bash in Node by invoking the sh command using the -c flag.

The sh command uses your default bash interpreter, and the -c flag prompts the interpreter to read commands from a string, that is: $(echo $PATH) . Then additional flags are passed to their usual positional links, such as: $0 , $1 , ect.

So an example could be:

 const spawn = require('child_process').spawn; const prg = 'sh', args = [ '-c', 'echo $($0 $1)', 'ls', // $0 '-la' // $1 ], opts = { stdio: 'inherit' }; // Print a directory listing, Eg: 'ls -la' const child = spawn(prg, args, opts); 
+1
source

you can <<50> evaluate the bash -c 'command' . I tested this, and it works with process substitution and child_process.

0
source

Source: https://habr.com/ru/post/1493452/


All Articles