'Check if rpm exists in bash script silently

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



Solution 1:[1]

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

Solution 2:[2]

You need only -q option actually:

$ rpm -q zabbix-agent

package zabbix-agent is not installed

$ rpm -q curl

curl-7.24.0-5.25.amzn1.x86_64

It will look like:

$ if rpm -q zabbix-agent > /dev/null; then echo "Package zabbix-agent is already installed."; fi

Package zabbix-agent is already installed.

Solution 3:[3]

You could do:

[ -z "$(rpm -qa|grep glib)" ] && echo none || echo present

...or, if you prefer:

if [ $(rpm -qa|grep -c glib) -gt 0 ]; then
    echo present
else
    echo none
fi

Solution 4:[4]

You could test if the command returns a string, the command substitution will capture the output:

[[ "$(rpm -qa | grep glib)" ]] && do something

Solution 5:[5]

curl_setopt(CurlHandle $handle, int $option, mixed $value): bool

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ztank1013
Solution 2 fedorqui
Solution 3 JRFerguson
Solution 4 ata
Solution 5 manish srivastava