Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for existence of wget/curl

Tags:

linux

shell

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?

like image 811
John Williams Avatar asked Jan 19 '13 04:01

John Williams


2 Answers

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, so you could use something like:

if which wget >/dev/null ; then
    echo "Downloading via wget."
    wget --option argument
elif which curl >/dev/null ; then
    echo "Downloading via curl."
    curl --option argument
else
    echo "Cannot download, neither wget nor curl is available."
fi
like image 130
paxdiablo Avatar answered Sep 23 '22 07:09

paxdiablo


wget http://download/url/file 2>/dev/null || curl -O  http://download/url/file
like image 35
Suku Avatar answered Sep 26 '22 07:09

Suku