在spawn中使用两个命令(使用pipe |) [英] Using two commands (using pipe |) with spawn
问题描述
我正在将内存中的doc转换为pdf(unoconv),并在终端中使用以下命令打印(pdftotext):
I'm converting a doc to a pdf (unoconv) in memory and printing (pdftotext) in the terminal with:
unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt
正在工作.现在我想将此命令与child_process.spawn
一起使用:
Is working. Now i want use this command with child_process.spawn
:
let filePath = "...",
process = child_process.spawn("unoconv", [
"-f",
"pdf",
"--stdout",
filePath,
"|",
"pdftotext",
"-layout",
"-enc",
"UTF-8",
"-",
"-"
]);
在这种情况下,只有第一个命令(在|之前)有效.我可以做我想做的事吗?
In this case, only the first command (before the |) is working. Is i possible to do what i'm trying?
谢谢.
更新-
结果:sh -c- ....
bash-3.2$ sh -c- unoconv -f pdf --stdout /Users/fatimaalves/DEV/xx/_input/sample.doc | pdftotext -layout -enc UTF-8 - -
sh: --: invalid option
Usage: sh [GNU long option] [option] ...
sh [GNU long option] [option] script-file ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--restricted
--verbose
--version
--wordexp
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table
推荐答案
如果您不想使用上述的sh
命令,则必须创建多个child_process.spawn实例,然后像使用管道一样将它们相互连接所以:
If you don't want to use the sh
command as explained above, you must create multiple child_process.spawn instances and then pipe them into each other like so:
const getModule = spawn('curl', [url, '-ks']);
const unTar = spawn('tar', ['-xvz', '-C', fileName, '--strip-components', 1]);
getModule.stdout.pipe(unTar.stdin);
理论上,以上代码将从url
中检索tar,并将其解压缩到目录fileName
The above code theoretically will retrieve a tar from url
, and unpack into a directory fileName
这篇关于在spawn中使用两个命令(使用pipe |)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!