如何从 Github 执行 Bash 脚本?

Arc*_*ing 15 command-line bash scripts downloads git

说这个,我my_cool_file.shGithub(或BitBucket)的 Git 存储库中有以下文件名为my_cool_repo. 该文件是用于安装 ConfigServer 著名的 CSF-LFD 软件的脚本:

#!/bin/bash
cd /usr/src
rm -fv csf.tgz
wget https://download.configserver.com/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
sed -i "s/TESTING = "1"/TESTING = "0"/g" /etc/csf/csf.conf
csf -r
perl /usr/local/csf/bin/csftest.pl
# sh /etc/csf/uninstall.sh
Run Code Online (Sandbox Code Playgroud)

如何.sh通过命令行直接从 Github执行这个 Bash 脚本(一个文件)?

Vid*_*uth 25

wget使用其确切的 URL加载文件(确保使用原始文件,否则加载 HTML 页面!),然后将输出通过管道传输到bash

这是一个带有说明的示例:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | bash
Run Code Online (Sandbox Code Playgroud)

命令联机帮助页wget

   -O file
   --output-document=file
       The documents will not be written to the appropriate files, but all
       will be concatenated together and written to file.  If - is used as
       file, documents will be printed to standard output, disabling link
       conversion.  (Use ./- to print to a file literally named -.)

       Use of -O is not intended to mean simply "use the name file instead
       of the one in the URL;" rather, it is analogous to shell
       redirection: wget -O file http://foo is intended to work like wget
       -O - http://foo > file; file will be truncated immediately, and all
       downloaded content will be written there.
Run Code Online (Sandbox Code Playgroud)

因此,输出到-实际上会将文件内容写入 STDOUT,然后您只需将其通过管道传输到bash您喜欢的任何 shell。如果您的脚本需要sudo权限,您需要sudo bash在最后执行,因此该行变为:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | sudo bash
Run Code Online (Sandbox Code Playgroud)

  • 参数呢?假设管道脚本可以选择处理 $1,是否有办法将其传入? (2认同)

小智 7

如果您的脚本有像这样的用户条目,read user_input请尝试以下操作:

bash <(curl -s https://raw.githubusercontent.com/username/project/branch/path/file.sh )

-s参数不显示下载进度。