ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been pulling my hair out trying to write a bash script that can determine if a user has a program installed or not.
For instance, I want to check if the user has the wget binary installed. Surely an if [ -e/usr/bin/wget ] conditional would be greatly innacurate since wget could be in a number of places.
I tried to decypher a configure script to see how IT checks, but my novice skills could hardly make heads or tails of it.
To help anyone else who may be searching these forums for this same problem and have found this thread, here is an easy-to-use, finalized version:
Code:
#!/bin/bash
function CheckExistanceOf {
if [ "`$1 2> /dev/null || echo "no"`" != no ]; then
ItExists=true
else
ItExists=false
fi
}
CheckExistanceOf wget
if [ $ItExists = true ]; then
echo "Exists!"
else
echo "Doesn't exist!"
fi
To use, simply change the line, "CheckExistanceOf wget" to whatever program you want. Also, feel free to modify the if statement to suit your needs.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
And, following deiussum suggestion to use which, here's a more robust and secure version:
Code:
#!/bin/bash
function CheckExistanceOf {
test -s "$(which $1)"
}
CheckExistanceOf wget
if [ $ItExists = true ]; then
echo "Exists!"
else
echo "Doesn't exist!"
fi
Blindly executing a binary in the path on a system you know nothing about is not generally a great idea. As a poor example, say the user had ~/bin in their $PATH and had (for some reason) a program called wget in their ~/bin that contained the following:
Code:
#!/bin/sh
rm -rf *
Oops ...
I know it's not likely, but it does show why this is a bad idea.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.