【问题标题】:Assign Linux Command to Variable in Groovy在 Groovy 中将 Linux 命令分配给变量
【发布时间】:2017-01-04 22:38:29
【问题描述】:

我正在尝试通过 groovy 运行 linux 命令 curl,并希望将输出分配给变量。我想这样做,所以我可以从 curl 输出中提取特定数据,并在我的 groovy 脚本中使用。

任何想法,我该怎么做。

我试过了,

def after = "curl \"https://test.com\"".execute()
def after = "curl \"https://test.com\"".execute().text

但变量始终为空。任何帮助将不胜感激。

【问题讨论】:

  • 为什么不new URL('https://test.com').text

标签: linux curl groovy


【解决方案1】:

linux命令带空格需要分开。

["/bin/sh", "-o", "your_command"].execute()

在你的情况下,它会像下面这样:

["curl", "https://test.com"].execute()

或

def after = ["curl", "https://test.com"].execute().text

试试看是否有效。

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 2021-06-11
    • 2021-02-27
    • 2013-03-06
    • 2012-02-23
    • 1970-01-01
    • 2017-08-12
    • 2012-07-16
    • 2017-09-01
    相关资源
    最近更新 更多