使用两个命令(使用pipe道|)产生

我正在将文档转换为内存中的pdf(unoconv),并在terminal中打印(pdftotext):

unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt 

工作中。 现在我想用child_process.spawn使用这个命令:

 let filePath = "...", process = child_process.spawn("unoconv", [ "-f", "pdf", "--stdout", filePath, "|", "pdftotext", "-layout", "-enc", "UTF-8", "-", "-" ]); 

在这种情况下,只有第一个命令(在|之前)正在工作。 我可以做我想要的吗?

谢谢。

最新情况:

结果: 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 

所有以pipe道开头的东西都不是unoconv的参数。 它由shell处理,而不是由unoconv 。 所以你不能把它作为unoconv中的参数数组的一部分来unoconv

有很多方法可以解决这个问题,这取决于你的需求。 如果您知道您只会在类UNIX操作系统上运行,则可以将您的命令作为parameter passing给sh

 process = child_process.spawn('sh', ['-c', 'unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt']);