【问题标题】:Using two commands (using pipe |) with spawn使用带有 spawn 的两个命令(使用管道 |)
【发布时间】:2016-11-11 09:53:18
【问题描述】:

我正在将内存中的文档转换为 pdf (unoconv) 并在终端中打印 (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

【问题讨论】:

  • 不是sh -c-。这是sh -c

标签: node.js child-process spawn pdftotext unoconv


【解决方案1】:

以管道开头的所有内容都不是unoconv 的参数。它由 shell 处理,而不是由unoconv 处理。所以你不能将它作为unoconv 中参数数组的一部分传递。

有很多方法可以解决这个问题,具体取决于您的需要。如果您知道您将只在类 UNIX 操作系统上运行,您可以将您的命令作为参数传递给 sh

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

【讨论】:

    【解决方案2】:

    如果您不想像上面解释的那样使用sh 命令,则必须创建多个 child_process.spawn 实例,然后像这样将它们相互传递:

    const getModule = spawn('curl', [url, '-ks']);
    const unTar = spawn('tar', ['-xvz', '-C', fileName, '--strip-components', 1]);
    getModule.stdout.pipe(unTar.stdin);
    

    上面的代码理论上会从url获取一个tar,解压到一个目录fileName

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2012-10-31
      • 1970-01-01
      • 2016-08-22
      • 2015-04-12
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多