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

标签 bash gradle github build.gradle

假设你有一个简单的 bash 脚本

echo $@

将其托管在公共(public)存储库中,因此您可以访问原始文件,例如

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

然后你可以在 shell 中运行它
bash <(curl -s https://raw.githubusercontent.com/.../test.sh) "hello"

我希望能够在 gradle 任务中实现这一目标。我试过了:
task testScript(type: Exec) {
  workingDir projectDir
  executable "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) 'hello'"
}


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


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

无济于事。我究竟做错了什么?

最佳答案

在您的原始命令中,<(curl -s https://raw.githubusercontent.com/.../test.sh) bash 无法解决您调用的命令,但通过您调用它的 shell,实际执行的命令类似于 bash /dev/fd/63 "hello" .

Gradle 不是外壳,只会调用 bash使用字符串 "<(curl -s https://raw.githubusercontent.com/.../test.sh)"作为参数,无需进一步处理。

您需要找到一个不需要被调用它的 shell 扩展的命令。例如,将您当前的命令作为纯文本处理并使用另一个 shell 来解决它:

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

总之,我相信以下应该有效:
task testScript(type: Exec) {
  workingDir projectDir
  executable "bash"
  args "-c", "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) hello"
}

关于bash - 如何运行从 Gradle 任务远程托管的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60190278/

相关文章:

bash - 如何用 0 替换不存在的值?

bash - 使用 Control C 防止 shell 脚本退出

bash - 计算 bash 中的特定单词

spring-boot - Spring-Boot入门测试导入未解决

gradle - 如何在构建期间排除gradle任务或方法

gradle - 以类型安全的方式获取Gradle任务列表

linux - 将部分字符串转换为另一个字符集

button - Github 桌面窗口同步按钮消失,历史选项卡不工作

github - 如何设置github只读访问

Git:如何在存储库之间移动分支