Check if rpm exists in bash script silently

Danny picture Danny · Nov 10, 2011 · Viewed 27.4k times · Source

I'm trying to do a quick check to see if an rpm is installed in a bash script using an if statement. But I want to do it silently. Currently, when I run the script, and the rpm does exist, it outputs the output of rpm to the screen which I dont want.

if rpm -qa | grep glib; then
    do something
fi

Maybe there is an option to rpm that I am missing? or if I just need to change my statement?

THanks

Answer

ztank1013 picture ztank1013 · Nov 10, 2011

There is the interesting --quiet option available for the rpm command. Man page says:

   --quiet
          Print  as little as possible - normally only error messages will
          be displayed.

So probably you may like to use this one:

if rpm -q --quiet glib ; then 
  do something 
fi

This way should be faster because it doesn't have to wait a -qa (query all) rpm packages installed but just queries the target rpm package. Of course you have to know the correct name of the package you want to test if is installed or not.

Note: using RPM version 4.9.1.2 on fedora 15