尝试执行脚本以使用 wget 下载文件,或者如果 Linux 中不存在 wget,则使用 curl。如何让脚本检查 wget 是否存在?
问问题
16922 次
6 回答
10
Linux 有一个which
命令可以检查路径上是否存在可执行文件:
pax> which ls ; echo $?
/bin/ls
0
pax> which no_such_executable ; echo $?
1
如您所见,它设置返回码$?
以轻松判断是否找到了可执行文件。
于 2013-01-19T04:32:34.910 回答
8
wget http://download/url/file 2>/dev/null || curl -O http://download/url/file
于 2013-01-19T04:37:13.460 回答
5
也可以使用command
ortype
或hash
来检查 wget/curl 是否存在。这里的另一个线程 - “检查是否存在来自 Bash 脚本的程序”很好地回答了在 bash 脚本中使用什么来检查程序是否存在。
我会这样做 -
if [ ! -x /usr/bin/wget ] ; then
# some extra check if wget is not installed at the usual place
command -v wget >/dev/null 2>&1 || { echo >&2 "Please install wget or set it in your path. Aborting."; exit 1; }
fi
于 2013-02-21T16:09:54.963 回答
2
首先要做的是尝试wget
使用通常的包管理系统进行安装。它应该告诉你是否已经安装;
yum -y wget
否则只需启动如下命令
wget http://download/url/file
如果您没有收到任何错误,则可以。
于 2017-01-30T08:01:09.220 回答
1
取自 K3S 安装脚本 ( https://raw.githubusercontent.com/rancher/k3s/master/install.sh )的解决方案
function download {
url=$1
filename=$2
if [ -x "$(which wget)" ] ; then
wget -q $url -O $2
elif [ -x "$(which curl)" ]; then
curl -o $2 -sfL $url
else
echo "Could not find curl or wget, please install one." >&2
fi
}
# to use in the script:
download https://url /local/path/to/download
说明:它查找文件的位置wget
并检查那里是否存在文件,如果存在,它会执行脚本友好(即安静)下载。如果未找到 wget,它会curl
以类似的脚本友好方式进行尝试。
(请注意,该问题并未指定 BASH,但我的回答假定它。)
于 2020-02-27T23:32:18.033 回答
0
只需运行
wget http://download/url/file
您将看到端点是否可用的统计信息。
于 2021-03-18T09:56:17.580 回答