I\'m working on a Ubuntu system and currently this is what I\'m doing:
if ! which command > /dev/null; then
echo -e \"Command not found! Install? (y/n)
$name="rsync"
[ `which $name` ] $$ echo "$name : installed" || sudo apt-get install -y $name
which <command>
if [ $? == 1 ]; then
<pkg-manager> -y install <command>
fi
This feature already exists in Ubuntu and Debian, in the command-not-found
package.
Use:
apt-cache policy <package_name>
If it is not installed, it will show:
Installed: none
Otherwise it will show:
Installed: version
apt list [packagename]
seems to be the simplest way to do it outside of dpkg and older apt-* tools
I offer this update since Ubuntu added its "Personal Package Archive" (PPA) just as this question was answered, and PPA packages have a different result.
Native Debian repository package not installed:
~$ dpkg-query -l apache-perl
~$ echo $?
1
PPA package registered on host and installed:
~$ dpkg-query -l libreoffice
~$ echo $?
0
PPA package registered on host but not installed:
~$ dpkg-query -l domy-ce
~$ echo $?
0
~$ sudo apt-get remove domy-ce
[sudo] password for user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package domy-ce is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Also posted on: https://superuser.com/questions/427318/test-if-a-package-is-installed-in-apt/427898