Check for existence of wget/curl

2019-04-11 06:00发布

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?

标签: linux shell
4条回答
霸刀☆藐视天下
2楼-- · 2019-04-11 06:35

First thing to do is try install to install wget with your usual package management system,. It should tell you if already installed;

yum -y wget

Otherwise just launch a command like below

wget http://download/url/file 

If you receive no error, then its ok.

查看更多
ら.Afraid
3楼-- · 2019-04-11 06:48
wget http://download/url/file 2>/dev/null || curl -O  http://download/url/file
查看更多
可以哭但决不认输i
4楼-- · 2019-04-11 06:52

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
查看更多
干净又极端
5楼-- · 2019-04-11 06:55

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.

查看更多
登录 后发表回答