试图做一个脚本来使用wget下载文件,或者如果在Linux中不存在wget,则会curl。 如何让脚本检查wget的存在?
wget http://download/url/file 2>/dev/null || curl -O http://download/url/file
Linux有一个命令将检查您的路径上的可执行文件的存在:
pax> which ls ; echo $? /bin/ls 0 pax> which no_such_executable ; echo $? 1
正如你所看到的,它设置返回码$?
轻松分辨是否找到可执行文件。
也可以使用command
或type
或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
首先要做的就是尝试安装安装wget
与你平常的包管理系统。 它应该告诉你,如果已经安装;
yum -y wget
否则,请启动一个如下所示的命令
wget http://download/url/file
如果你收到没有错误,那么确定。