为什么在groovy中执行该curl命令失败? [英] Why does this curl command fail when executed in groovy?

查看:213
本文介绍了为什么在groovy中执行该curl命令失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此curl命令在终端中有效,但无法进行常规操作。我添加了err并从另一个问题中退出,以尝试了解它为什么失败。

This curl command works in the terminal but fails in groovy. I added the err and out out from another question to try and understand why it fails.

def initialSize = 4096
def out = new ByteArrayOutputStream(initialSize)
def err = new ByteArrayOutputStream(initialSize)
def process = "sh -c curl'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts'".execute()
process.consumeProcessOutput(out, err)
process.waitFor()
println process.text
println err.toString()
println out.toString()

输出为 curl:尝试'curl --help'或'curl --manual'以获取更多信息

The output is "curl: try 'curl --help' or 'curl --manual' for more information"

推荐答案

不要使用字符串执行,因为Groovy会在空白处分割。您必须将 shell命令作为参数传递给 sh -c 。因此,现在您a)在 curl 和url之间缺少空格,并且b)这将作为两个参数结束(并且您不能为此引用)。

Don't use string for execute, as Groovy will split on whitespace. You have to pass the "shell command" as a single argument to sh -c. So right now you a) lack a space between curl and the url and b) this will end up as two arguments (and you can not quote for that).

改为使用字符串列表:

['sh', '-c', "curl 'http://...'"].execute()

也一个旁注:如果您只想要url的内容并且不需要花哨的内容(超时,身份验证,...),则可以这样做:

Also on a sidenote: if you just want the content of a url and don't need fancy things (timeouts, auth, ...), you can just as well do:

"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts".toURL().text

这篇关于为什么在groovy中执行该curl命令失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆