简体   繁体   English

如何运行从 Gradle 任务远程托管的 shell 脚本

[英]How to run a shell script hosted remotely from a Gradle task

Say you've got a simple bash script假设你有一个简单的 bash 脚本

echo $@

Hosted it in a public repo so you can access the raw file like将其托管在公共存储库中,以便您可以访问原始文件,例如

https://raw.githubusercontent.com/.../test.sh https://raw.githubusercontent.com/.../test.sh

Then you can run it in a shell like然后你可以在 shell 中运行它

bash <(curl -s https://raw.githubusercontent.com/.../test.sh) "hello"

I want to be able to achieve this within a gradle task.我希望能够在 gradle 任务中实现这一点。 I've tried:我试过了:

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) 'hello'"
}

and

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash"
  args "<(curl -s https://raw.githubusercontent.com/.../test.sh)" 'hello'
}

and

task testScript(type: Exec) {
  workingDir projectDir
  commandLine "bash", "<(curl -s https://raw.githubusercontent.com/.../test.sh)", "hello"
}

To no avail.无济于事。 What am I doing wrong?我究竟做错了什么?

In your original command, <(curl -s https://raw.githubusercontent.com/.../test.sh) isn't resolved by the bash command you call, but by the shell from which you call it, the actual command being executed is something like bash /dev/fd/63 "hello" .在您的原始命令中, <(curl -s https://raw.githubusercontent.com/.../test.sh)不是由您调用的bash命令解析的,而是由您调用它的 shell 解析的,正在执行的实际命令类似于bash /dev/fd/63 "hello"

Gradle is no shell and will just call bash with the string "<(curl -s https://raw.githubusercontent.com/.../test.sh)" as argument without further processing. Gradle 不是 shell,只会使用字符串"<(curl -s https://raw.githubusercontent.com/.../test.sh)"作为参数调用bash ,无需进一步处理。

You need to find a command that doesn't need to be expanded by the shell it's being called from.您需要找到一个不需要被调用它的 shell 扩展的命令。 For instance, handling your current command as plain text and using another shell to resolve it :例如,将当前命令作为纯文本处理并使用另一个 shell 来解析它:

bash -c 'bash <(curl -s https://raw.githubusercontent.com/.../test.sh) hello'

In conclusion I believe the following should work :总之,我认为以下应该有效:

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash"
  args "-c", "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) hello"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM