I'm trying to run a child process to modify a file (in two steps) before reading the modified content from stdout. I'm trying to do this by using process substitution which works perfectly in bash but not when i try it from node.
我正在尝试运行子进程来修改文件(分两步),然后再从stdout读取修改后的内容。我试图通过使用进程替换来做到这一点,它在bash中完美运行,但是当我从节点尝试时却没有。
This is kind of, what the command looks like..
这是什么,命令看起来像..
var p = exec('command2 <(capture /dev/stdout | command1 -i file -) -',
function (error, stdout, stderr) {
console.log(stderr);
});
stderr prints:
stderr打印:
/bin/sh: -c: line 0: syntax error near unexpected token `('
What is the proper way of doing this in node?
在节点中执行此操作的正确方法是什么?
3 个解决方案
#1
3
I solved this by putting the commands in a shell script and calling the script from the node child process. I also needed to add the following to set bash in posix mode to allow process substitution:
我通过将命令放在shell脚本中并从节点子进程调用脚本来解决这个问题。我还需要添加以下内容以在posix模式下设置bash以允许进程替换:
set +o posix
There is probably a nicer way of doing this directly from within node but it did the job. Cheers!
可能有更好的方法直接从节点内执行此操作,但它完成了这项工作。干杯!
#2
1
You can spawn
a child_process
with bash substitution in Node by invoking the sh
command with the -c
flag.
您可以通过使用-c标志调用sh命令在Node中生成带有bash替换的child_process。
The sh
command uses your default bash interpreter, and the -c
flag asks the interpreter to read commands from within the string, Ie: $(echo $PATH)
. Additional flags are then passed to their normal positional references, such as: $0
, $1
, ect.
sh命令使用默认的bash解释器,-c标志要求解释器从字符串中读取命令,即:$(echo $ PATH)。然后将其他标志传递给它们的正常位置引用,例如:$ 0,$ 1等。
So an example might 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);
#3
0
you can have bash evaluate a command with bash -c 'command'
. I've tested this and it works with process substitution and child_process.
你可以使用bash -c'命令'来bash评估一个命令。我测试了这个,它适用于进程替换和child_process。
#1
3
I solved this by putting the commands in a shell script and calling the script from the node child process. I also needed to add the following to set bash in posix mode to allow process substitution:
我通过将命令放在shell脚本中并从节点子进程调用脚本来解决这个问题。我还需要添加以下内容以在posix模式下设置bash以允许进程替换:
set +o posix
There is probably a nicer way of doing this directly from within node but it did the job. Cheers!
可能有更好的方法直接从节点内执行此操作,但它完成了这项工作。干杯!
#2
1
You can spawn
a child_process
with bash substitution in Node by invoking the sh
command with the -c
flag.
您可以通过使用-c标志调用sh命令在Node中生成带有bash替换的child_process。
The sh
command uses your default bash interpreter, and the -c
flag asks the interpreter to read commands from within the string, Ie: $(echo $PATH)
. Additional flags are then passed to their normal positional references, such as: $0
, $1
, ect.
sh命令使用默认的bash解释器,-c标志要求解释器从字符串中读取命令,即:$(echo $ PATH)。然后将其他标志传递给它们的正常位置引用,例如:$ 0,$ 1等。
So an example might 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);
#3
0
you can have bash evaluate a command with bash -c 'command'
. I've tested this and it works with process substitution and child_process.
你可以使用bash -c'命令'来bash评估一个命令。我测试了这个,它适用于进程替换和child_process。