I'm writing a bash script that needs to test if certain libs are installed on a VPS, and if they're not, the script will install them.

My problem is being able to have the script determine if they exist or not. My primary idea was to use this:

ldd /usr/lib/libgd.so > /tmp/lddcheck 2>/dev/null

To output the ldd results to a file, which I could then search with grep or something for the libs I'm looking for. If they're not there, I'll install them, otherwise the script will continue.

Not sure exactly how to do that. Any help would be appreciated (pretty new to bash scripting).


Given that you will be using some sort of package manager to install libraries, wouldn't it be much easier to just have a list of packages that you want to be installed and ensure somehow that they are installed?

100% of package managers that I know have some sort of mode (the default mode, usually) that either installs a package if it's not installed, updates it to the latest version if it's installed but old and just passes if it's already up to date.

For example, with apt-get you can always do apt-get install a b c d ... to make sure that packages a, b, c, d are installed. With yum that would be yum install a b c d, etc.

If you're using various systems with different package managers in your VPSes, you'll end up with writing some logic to detect which package manager does it use anyway, so this step is inevitable.


After you executed the ldd command as you suggested, you can try to use something like

grep "not found" /tmp/lddcheck | awk '// { print $1 }'

to get the names of the missing libraries.

  • Well, I guess I need to clarify a bit better. This is a script that I may use on a variety of VPS's, each of which may or may not have these particular libs installed. I know the two that may not be there, but I need the script to to be able to tell if a particular VPS has them installed or not. So I need to be able to put it into some kind of variable or something so that if "theselibs" returns false, then the script can install them.
    – Nate
    Jan 12 '12 at 3:00
  • 5
    It's kind of silly to pipe grep into an awk with an empty search; you can just do awk '/not found/ {print $1}' /tmp/lddcheck. Or grep 'not found' /tmp/lddcheck | cut -d' ' -f1 Jan 12 '12 at 3:59

Assuming the libraries you need support it, you should use pkg-config, this is what it's made for.

$ pkg-config --exists libnotify 
$ echo $?
$ pkg-config --exists nonexistant
$ echo $?

You can use it to check the version, tell you the include or libs paths, etc.


ldd $YOURBINARY 2>&1 |grep "missing" should output the missing libraries for your binary.


If you're new to scripting, remember this: you rarely need a temporary file; when you need to do several stages of processing on some data, reach for a pipe. There are many text processing tools you can use on the output of ldd. Here, I'll use awk, to detect lines where the third whitespace-separated field is not (as opposed to a path to a library), and print the first field (the library name). I'm collecting the output into a variable which will contain a whitespace-separated list of the missing libraries.

missing=$(ldd /path/to/executable | awk '$3 == "not" {print $1}')
for x in $missing; do
  # whatever it takes to install $x

Note that this is probably not the right approach. If you're going to have support for specific distributions, then don't make a generic binary package with custom interaction with the package manager. Distribute a package for that distribution.

Package managers make system administrators' life a lot easier; not playing well with the package manager will leave your users confused and angry. If you distribute rpms and debs and so on, your users will simply need to install the dependencies of your package. This way, your package will be recorded and can be installed, upgraded and uninstalled using the user's habitual package management tool rather than by executing some dubious script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.