Checking for installed packages and if not found install

amanada.williams picture amanada.williams · Oct 9, 2012 · Viewed 39k times · Source

I need to check for installed packages and if not installed install them.

Example for RHEL, CentOS, Fedora:

rpm -qa | grep glibc-static
glibc-static-2.12-1.80.el6_3.5.i686

How do I do a check in BASH?

Do I do something like?

if [ "$(rpm -qa | grep glibc-static)" != "" ] ; then

And what do I need to use for other distributions? apt-get?

Answer

Gilles Quenot picture Gilles Quenot · Oct 9, 2012

Try the following code :

if ! rpm -qa | grep -qw glibc-static; then
    yum install glibc-static
fi

or shorter :

rpm -qa | grep -qw glibc-static || yum install glibc-static

For debian likes :

dpkg -l | grep -qw package || apt-get install package

For archlinux :

pacman -Qq | grep -qw package || pacman -S package