12

Possible Duplicate:
How can I check if a package is installed (no superuser privileges)

I want a shell script method to test/report if a package is installed. I don't need details, only a yes/no.

I've come up with this method. Is there a more direct way?

is_installed=0
test_installed=( `apt-cache policy domy-ce | grep Installed: ` )
[ ! "${test_installed[1]}" == "(none)" ] && is_installed=1
tahoar
  • 265
  • 1
    This post should help you- http://stackoverflow.com/questions/1298066/check-if-a-package-is-installed-and-then-install-it-if-its-not – saji89 May 22 '12 at 07:18
  • Thanks saji89. The apparent duplicate gives accurate results with Debian registered packages, the results are unreliable on Ubuntu PPA packages. With packages that are registered on the host but not installed, dpkg's returncode is zero [0]. – tahoar May 23 '12 at 13:04

1 Answers1

19

You could use the output of dpkg -s <packagename> or dpkg-query -l <packagename>

in your script for the purpose.
Courtesy:https://stackoverflow.com/questions/1298066/check-if-a-package-is-installed-and-then-install-it-if-its-not

e.g.

#!/bin/sh

for P; do
    dpkg -s "$P" >/dev/null 2>&1 && {
        echo "$P is installed."
    } || {
        echo "$P is not installed."
    }
done

Usage: script.sh package1 package2 .... packageN

Courtesy:https://stackoverflow.com/a/10594734/749232

saji89
  • 12,007
  • 1
    If a package is installed then removed, then dpkg -s still exits with a 0 status! See http://askubuntu.com/a/395278/165556 – 200_success Oct 08 '15 at 22:49