简体   繁体   English

使用 curl 从 GitHub 运行 bash 脚本

[英]Run bash script from GitHub with curl

I want to run a script from GitHub with curl, and I can do this easily with:我想从 GitHub 和 curl 运行一个脚本,我可以很容易地做到这一点:

# This is safe to run but will add a few lines to .bashrc, .vimrc, .inputrc to add some configuration options.
# Please check the project before running:
# https://raw.github.com/roysubs/custom_bash/
# But this is just to illustrate the point, bash shell script on GitHub, and how to use curl to pipe it to bash
curl -s https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh | bash

I have a few problems with this however that I was hoping for some help with:我对此有一些问题,但是我希望得到一些帮助:

  • Firstly, the script always runs unattended, ignoring all read -e -p statements to take input from the console.首先,脚本总是在无人看管的情况下运行,忽略所有read -e -p语句以从控制台获取输入。 I'd appreciate if there is a better way to run the script such that it does not do this.如果有更好的方法来运行脚本以使其不执行此操作,我将不胜感激。 Other methods to do this would be great.其他方法会很棒。
  • Secondly, I used git.io to shorten the above url to https://git.io/Jt0fZ but I can never get the above curl method to work using this. Secondly, I used git.io to shorten the above url to https://git.io/Jt0fZ but I can never get the above curl method to work using this. ie curl -s https://git.io/Jt0fZ | bashcurl -s https://git.io/Jt0fZ | bash curl -s https://git.io/Jt0fZ | bash Does anyone know why this fails and for a workaround to use the shortened url to execute the remote script? curl -s https://git.io/Jt0fZ | bash有谁知道为什么这会失败,以及使用缩短的 url 执行远程脚本的解决方法?

The "Firstly" is more complex, but you might try a temporary shell script without using pipe. “首先”更复杂,但您可以尝试使用临时 shell 脚本而不使用 pipe。

curl -s https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh >tmp.sh

bash tmp.sh

Your "Secondly" question is very easy to explain, when you do curl -s https://git.io/Jt0fZ , we get a Redirect 302, here is hints:您的“第二”问题很容易解释,当您执行curl -s https://git.io/Jt0fZ时,我们会得到一个重定向 302,这里是提示:

We try to get headers only from the server:我们尝试仅从服务器获取标头:

curl --head https://git.io/Jt0fZ

HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
Date: Sun, 25 Apr 2021 14:24:43 GMT
Status: 302 Found
Content-Type: text/html;charset=utf-8
Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh
Content-Length: 0
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.003242
X-Node: 69d60833-56c5-4364-94c1-6f0c5b12863f
X-Revision: 392798d237fc1aa5cd55cada10d2945773e741a8
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur

We see HTTP/1.1 302 Found and Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh , not the real content of the latter.我们看到HTTP/1.1 302 Found and Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh ,不是后者的真实内容。

So bash gets empty script to run, by default, curl does not do a second call to the new location, as does graphic webbrowsers usually do.所以 bash 得到空脚本来运行,默认情况下,curl 不会像图形网络浏览器通常那样对新位置进行第二次调用。

But you can do this to overcome the default:但是您可以这样做来克服默认值:

curl -L https://git.io/Jt0fZ | bash

man curl gives this:人 curl 给出了这个:

-L, --location (HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place. -L, --location (HTTP) 如果服务器报告请求的页面已移动到不同的位置(用 Location: header 和 3XX 响应代码指示),此选项将使 curl 在新位置重做请求。

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

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