我有一个 bash 文件,我将其远程保存,并且以下步骤正常运行。
curl -fsSL -o check.sh https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh
chmod 700 check.sh
sudo ./check.sh
但是,如果我将其更改为一行命令,它将不起作用/不显示任何输出。
curl https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh | bash
实际的代码是:
#!/usr/bin/env bash
count_apt=`whereis apt`
count_yum=`whereis yum`
## echo ${#count_apt} ${#count_yum}
if (( ${#count_yum} > 4 )); then
echo "---Redhat distributions are not supported---"
exit 0
fi
if (( ${#count_apt} > 4 )); then
echo "---Found Ubuntu Distribution---"
echo "---Started installation of dependencies---"
apt install -y curl
wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/agentxw_1.036-1_amd64.deb
apt install -y ./agentxw_1.036-1_amd64.deb
wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/xvision_0.97-1_amd64.deb
apt install -y ./xvision_0.97-1_amd64.deb
echo "---Completed installation of dependencies---"
echo "---Started installation of main package with its dependencies: xenowulf-ai---"
wget https://github.com/uday1kiran/python_deb_create/raw/testdeb2/tmp/xenowulf-ai_1.0_all.deb
apt install -y ./xenowulf-ai_1.0_all.deb
echo "---Completed installation of main pacakge: xenowulf-ai---"
fi
请提出建议。
答案1
您的第一个命令(有效的命令)使用 curl 的-L
选项(来自man 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 re‐
quest on the new place. [. . .]
您的 URL 的工作方式似乎是通过这种重定向,因为如果没有该-L
选项,您将什么也得不到:
$ url="https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh"
$ curl "$url" 2>/dev/null | wc
0 0 0
但有了它,你会得到输出:
$ curl -L "$url" 2>/dev/null
#!/usr/bin/env bash
count_apt=`whereis apt`
count_yum=`whereis yum`
## echo ${#count_apt} ${#count_yum}
if (( ${#count_yum} > 4 )); then
echo "---Redhat distributions are not supported---"
exit 0
fi
if (( ${#count_apt} > 4 )); then
echo "---Found Debian Distribution---"
echo "---Started installation of dependencies---"
apt install -y curl
wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/agentxw_1.036-1_amd64.deb
apt install -y ./agentxw_1.036-1_amd64.deb
wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/xvision_0.97-1_amd64.deb
apt install -y ./xvision_0.97-1_amd64.deb
echo "---Completed installation of dependencies---"
echo "---Started installation of main package with its dependencies: xenowulf-ai---"
wget https://github.com/uday1kiran/python_deb_create/raw/testdeb2/tmp/xenowulf-ai_1.0_all.deb
apt install -y ./xenowulf-ai_1.0_all.deb
echo "---Completed installation of main pacakge: xenowulf-ai---"
fi
因此您需要做的就是使用该-L
选项然后将其传递给sudo bash
(因为您需要以 root 身份运行它):
curl -L "$url" | sudo bash
当然,这是非常危险的,只有当您 100% 确定脚本安全时才可以运行此类操作。另外,如果您curl
使用curl
来下载脚本,检查 是否存在似乎很奇怪。您已经知道curl
可用。无论如何,尽管您正在测试curl
,但您的脚本实际上使用的是wget
。您还可以进行各种其他改进(反引号已弃用,请使用var=$(command)
代替var=`command`
;有更好的方法来检查发行版,因为 yum 可以安装在非基于 RedHat 的发行版和apt
非 Debian 的发行版上),但这些都不是这里的主题。