Check for existence of wget/curl

Trying to do a script to download a file using wget, or curl if wget doesn't exist in Linux. How do I have the script check for existence of wget?


wget http://download/url/file 2>/dev/null || curl -O  http://download/url/file

Linux has a which command which will check for the existence of an executable on your path:

pax> which ls ; echo $?
/bin/ls
0

pax> which no_such_executable ; echo $?
1

As you can see, it sets the return code $? to easily tell if the executable was found.


One can also use command or type or hash to check if wget/curl exists or not. Another thread here - "Check if a program exists from a Bash script" answers very nicely what to use in a bash script to check if a program exists.

I would do this -

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
链接地址: http://www.djcxy.com/p/57072.html

上一篇: 如何将命令行参数传递给shell别名?

下一篇: 检查是否存在wget / curl