【问题标题】:How can I use Groovy to execute a shell command that has backticks?如何使用 Groovy 执行带有反引号的 shell 命令?
【发布时间】:2013-01-11 16:17:17
【问题描述】:

我无法使用 Groovy 执行带有反引号的 shell 命令。一个简化的例子:

println "echo `date`".execute().text

我四处寻找并试图弄清楚如何以某种方式逃脱它们,但没有运气。

【问题讨论】:

    标签: bash shell groovy backticks


    【解决方案1】:

    如果你尝试会发生什么:

    println ["bash", "-c", "echo `date`"].execute().text
    

    我的猜测是

    "echo `date`".execute() 
    

    如果您在字符串上调用execute(),则将在下面使用java 的Runtime#exec(String)。在这种情况下,这只是标记字符串并使用参数执行程序echo

    `date`
    

    $(date)
    

    但这是 shell (bash) 语法,必须通过 bash 执行。

    【讨论】:

    • 几乎,println 对此有问题,但将其分成两行有效:def process = ["bash", "-c", command].execute(); println process.text
    猜你喜欢
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    相关资源
    最近更新 更多