在groovy run process failure中,命令在文件arg中有空格

时间:2017-06-26 14:18:13

标签: java bash groovy process

这很不重要。当我执行包含具有嵌入空格的文件参数的命令时,该命令失败。但是,我可以 pwd 。我也可以从命令行运行完全相同的命令。这是在Linux Mint上运行的 - 再次,并不是它应该重要。这是我日志中的一个片段,显示了pwd命令和第二个命令(flac piped to lame)。

2017-06-26T08:50:00.750 runCommand pwd, /home/worldwidewilly/Music/album-rip/Dixie Cups, The
2017-06-26T08:50:00.789 OUT: /home/worldwidewilly/Music/album-rip/Dixie Cups, The

2017-06-26T08:50:00.790 ERROR: 
2017-06-26T08:50:00.791 runCommand flac -cd "Iko Iko".flac | lame -b 320 - "Iko Iko".mp3, /home/worldwidewilly/Music/album-rip/Dixie Cups, The
2017-06-26T08:50:00.794 OUT: 
2017-06-26T08:50:00.795 ERROR: 
flac 1.3.0, Copyright (C) 2000-2009, 2011-2013  Josh Coalson & Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.


"Iko: ERROR initializing decoder
      init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

Iko".flac: ERROR initializing decoder
           init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

|: ERROR initializing decoder
   init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

lame: ERROR initializing decoder
      init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

"Iko: ERROR initializing decoder
      init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

Iko".mp3: ERROR initializing decoder
          init status = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE

An error occurred opening the input file; it is likely that it does not exist
or is not readable.

它实际上似乎不喜欢flac或lame命令中歌曲名称中的嵌入空格。它似乎也不喜欢管道。

有问题的命令从命令行运行得很好。

worldwidewilly@hal9000 ~/Music/album-rip/Dixie Cups, The $ flac -cd "Iko Iko".flac | lame -b 320 - "Iko Iko".mp3

flac 1.3.0, Copyright (C) 2000-2009, 2011-2013  Josh Coalson & Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.

Iko Iko.flac: done         
LAME 3.99.5 64bits (http://lame.sf.net)
Using polyphase lowpass filter, transition band: 20094 Hz - 20627 Hz
Encoding <stdin> to Iko Iko.mp3
Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (4.4x) 320 kbps qval=3

修改

使用 sh -c 响铃。我在六七年前的Gradle脚本中使用它。但是,我所做的仍然不起作用。我相信我按照建议做。现在,您将从下面的日志中注意到我得到 -cd:1:-cd:语法错误:未终止的引用字符串。做一些研究,似乎shell正在进行单词分裂。不知道我应该怎么做。我看到的解决方案并不适用于JVM,似乎不合适。

这是我的代码(我最初没有包含):

def mp3FromFlac(flac) {
    log "mp3FromFlac ${flac.name}"
    def name = flac.name - '.flac'
    def path_to_song = flac.parentFile.absolutePath
    def flacCmd = /flac -cd "${name}".flac/
    def lameCmd = /lame -b 320 -q 0 - "${name}".mp3/

    log "mp3FromFlac: flacCmd: ${flacCmd}"
    log "mp3FromFlac: lameCmd: ${lameCmd}"
    def cmd = /sh -c '${flacCmd} | ${lameCmd}'/
    log "mp3FromFlac: cmd: ${cmd}"
    def proc = cmd.execute(null, new File(path_to_song))
    log "mp3FromFlac: OUT: ${proc.in.text}"
    log "mp3FromFlac: ERROR: ${proc.err.text}"
}

而且,这是我当前的日志文件:

2017-06-27T15:12:42.436 mp3FromFlac Iko Iko.flac
2017-06-27T15:12:42.437 mp3FromFlac: flacCmd: flac -cd "Iko Iko".flac
2017-06-27T15:12:42.437 mp3FromFlac: lameCmd: lame -b 320 -q 0 - "Iko Iko".mp3
2017-06-27T15:12:42.437 mp3FromFlac: cmd: sh -c 'flac -cd "Iko Iko".flac | lame -b 320 -q 0 - "Iko Iko".mp3'
2017-06-27T15:12:42.479 mp3FromFlac: OUT: 
2017-06-27T15:12:42.480 mp3FromFlac: ERROR: -cd: 1: -cd: Syntax error: Unterminated quoted string

2 个答案:

答案 0 :(得分:0)

由于您没有指定您是如何尝试从Groovy调用它的,我只是测试了一下以向您展示变体:

/cat foo bar/.execute().err.text

Result: cat: foo: No such file or directory
cat: bar: No such file or directory
/cat 'foo bar'/.execute().err.text

Result: cat: 'foo bar': No such file or directory
/cat 'foo bar' | wc -l/.execute().err.text

Result: cat: unknown option -- l
Try 'cat --help' for more information.
/sh -c 'cat "foo bar" | wc -l'/.execute().err.text

Result: cat: 'foo bar': No such file or directory
(/cat 'foo bar'/.execute() | /wc -l/.execute()).in.text

Result: 0

因此,如果您使用引号,它们会正确地转义空格。但是你当然不能使用管道,因为这是一个shell构造。所以你要么必须在sh -c中打包你的电话,要么在Groovy中管道而不是你的命令。

答案 1 :(得分:0)

所以,感谢@Vampire和@dagget,我有足够的信息来解决这个问题。这个SO using-groovy-how-do-you-pipe-multiple-shell-commands填补了缺失的部分。我以前做过这个,但很久以前。无论如何,这是我编码的:

def cmd = ['/bin/bash', '-c', /flac -cd "${name}".flac | lame -b 320 -q 0 - "${name}".mp3/]
log "mp3FromFlac: cmd: ${cmd}"

proc = cmd.execute(null, new File(path_to_song))

是的,我可以在flac和lame命令中包含路径,但是因为我已经有了这个,所以这不是问题。