英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论