Linux有一个which命令可以检查路径上是否存在可执行文件:
pax> which ls ; echo $?
/bin/ls
0
pax> which no_such_executable ; echo $?
1
Run Code Online (Sandbox Code Playgroud)
如您所见,它设置返回代码$?以轻松判断是否找到了可执行文件.
wget http://download/url/file 2>/dev/null || curl -O http://download/url/file
Run Code Online (Sandbox Code Playgroud)
还可以使用commandortype或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
Run Code Online (Sandbox Code Playgroud)