【发布时间】:2013-01-11 16:17:17
【问题描述】:
我无法使用 Groovy 执行带有反引号的 shell 命令。一个简化的例子:
println "echo `date`".execute().text
我四处寻找并试图弄清楚如何以某种方式逃脱它们,但没有运气。
【问题讨论】:
标签: bash shell groovy backticks
我无法使用 Groovy 执行带有反引号的 shell 命令。一个简化的例子:
println "echo `date`".execute().text
我四处寻找并试图弄清楚如何以某种方式逃脱它们,但没有运气。
【问题讨论】:
标签: bash shell groovy backticks
如果你尝试会发生什么:
println ["bash", "-c", "echo `date`"].execute().text
我的猜测是
"echo `date`".execute()
如果您在字符串上调用execute(),则将在下面使用java 的Runtime#exec(String)。在这种情况下,这只是标记字符串并使用参数执行程序echo
`date`
或
$(date)
但这是 shell (bash) 语法,必须通过 bash 执行。
【讨论】:
def process = ["bash", "-c", command].execute(); println process.text