从Java/Groovy进程调用ZSH命令

huangapple go评论59阅读模式
英文:

Invoking ZSH commands from Java/Groovy Process

问题

运行以下Groovy代码时出现错误: unmatched '

Process process = "zsh -c 'ls -l'".execute()

然而,以下代码正常工作Process process = "zsh -c ls".execute()

如何调用接受多个标志的zsh命令?

英文:

Running the following Groovy code is giving me error : unmatched '

Process process = "zsh -c 'ls -l'".execute()

However, the following works fine Process process = "zsh -c ls".execute().

How to invoke a zsh command which takes multiple flags?

答案1

得分: 3

永远不要使用String.execute() - 它会在空白字符上分割,并且只适用于非常简单的命令。无论如何,对字符串进行引用的尝试都是徒劳的,因为这里没有使用shell来解析字符串。

请始终改用List.execute()。例如:

["zsh", "-c", "ls -1"].execute()
英文:

Never ever use String.execute() - it will split on whitespace and is only sane for very simple commands. The attempt in quoting is in vain in any case, because no shell is used here to parse the string.

Always use List.execute() instead. E.g.

["zsh", "-c", "ls -1"].execute()

huangapple
  • 本文由 发表于 2020年7月22日 09:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63025566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定