Shell – Check if all listed packages are installed in bash

aptdpkgshell

My binary depends on these boost libraries and respective packages:

NAME                                  |          PACKAGE:
--------------------------------------+-----------------------------------
libboost_serialization.so.1.55.0      |  libboost-serialization1.55.0
libboost_thread.so.1.55.0             |  ibboost-thread1.55.0
libboost_date_time.so.1.55.0          |  libboost-date-time1.55.0
libboost_signals.so.1.55.0            |  libboost-signals1.55.0

So I'd like to define a list of packages:

boostlibnames="libboost-serialization1.55.0 libboost-thread1.55.0 libboost-date-time1.55.0 libboost-signals1.55.0"

And now I'd like to check if they are all installed and only then run actual apt-get install. I know apt will automatically check whether the packages are already installed, but I'd like to place in a prompt to the user whether he wants to install missing libraries before doing so.

So is there a nice way to check whether all listed libraries (in variable as above) are installed?

Best Answer

The dpkg -s command returns the status of installed packages. For example, on my system, if I run it for firefox which is installed and nedit which isn't, I get:

$ dpkg -s firefox
Package: firefox
Status: install ok installed
Priority: optional
Section: web
Installed-Size: 94341
Maintainer: Clement Lefebvre <root@linuxmint.com>
Architecture: amd64
Version: 41.0~linuxmint1+betsy
Replaces: firefox-l10n-af, firefox-l10n-ar, firefox-l10n-be, firefox-l10n-bg, firefox-l10n-bn-bd, firefox-l10n-ca, firefox-l10n-cs, firefox-l10n-da, firefox-l10n-de, firefox-l10n-el, firefox-l10n-en-gb, firefox-l10n-en-us, firefox-l10n-eo, firefox-l10n-es, firefox-l10n-et, firefox-l10n-eu, firefox-l10n-fa, firefox-l10n-fi, firefox-l10n-fr, firefox-l10n-fy, firefox-l10n-gl, firefox-l10n-gu, firefox-l10n-he, firefox-l10n-hi, firefox-l10n-hr, firefox-l10n-hu, firefox-l10n-id, firefox-l10n-is, firefox-l10n-it, firefox-l10n-ja, firefox-l10n-kn, firefox-l10n-ko, firefox-l10n-lt, firefox-l10n-lv, firefox-l10n-nb, firefox-l10n-nl, firefox-l10n-nn, firefox-l10n-pl, firefox-l10n-pt, firefox-l10n-pt-br, firefox-l10n-ro, firefox-l10n-ru, firefox-l10n-sk, firefox-l10n-sl, firefox-l10n-sq, firefox-l10n-sr, firefox-l10n-sv, firefox-l10n-th, firefox-l10n-tr, firefox-l10n-uk, firefox-l10n-zh
Provides: gnome-www-browser, www-browser
Breaks: firefox-l10n-en-us
Description: The Firefox web browser
 The Mozilla Firefox Web Browser.

$ dpkg -s nedit
dpkg-query: package 'nedit' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.

So, you can use that command to check whether a package is installed:

#!/usr/bin/env bash

run_install()
{
    ## Prompt the user 
    read -p "Do you want to install missing libraries? [Y/n]: " answer
    ## Set the default value if no answer was given
    answer=${answer:Y}
    ## If the answer matches y or Y, install
    [[ $answer =~ [Yy] ]] && apt-get install ${boostlibnames[@]}
}

boostlibnames=("libboost-serialization1.55.0" "libboost-thread1.55.0"
                "libboost-date-time1.55.0" "libboost-signals1.55.0" "nedit")
## Run the run_install function if sany of the libraries are missing
dpkg -s "${boostlibnames[@]}" >/dev/null 2>&1 || run_install
Related Question