java - 在 groovy 运行过程中,命令在文件 arg 中包含空格而失败

标签 java bash groovy process

这并不重要,这很酷。当我执行包含嵌入空格的文件参数的命令时,该命令失败。不过,我可以密码。我还可以从命令行运行完全相同的命令。这是在 Linux Mint 上运行的——同样,这并不重要。这是我日志中的一个片段,显示了 pwd 命令和第二个命令(flac 通过管道传输到 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

最佳答案

由于您没有指定如何尝试从 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 中而不是在命令中执行管道操作。

关于java - 在 groovy 运行过程中,命令在文件 arg 中包含空格而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761835/

相关文章:

java - 运行时不显示 JSF 页面

grails - Grails 如何使 `params` 可用于单例 Controller ? (Grails 内部结构)

java - 在groovy中扩展LinkedHashMap时如何覆盖equals

java - GPathResult ..存在或不存在节点

performance - ElasticSearch 检索文档速度缓慢

java - 构造函数可以在Java中私有(private)吗

Java - 根据不同纪元的外部设备的时间戳计算时间

bash - 使用 awk 分割带有多个字符串定界符的行

linux - 通过 Perl 脚本设置环境变量

bash ->/dev/zero 取消之前的追加