问题
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
回答1:
1) You can add -q switch to grep
if rpm -qa | grep -q glib; then
do something
fi
2) You can redirect stout and/or stderr output to /dev/null
if rpm -qa | grep glib 2>&1 > /dev/null; then
do something
fi
回答2:
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
回答3:
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.
回答4:
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
回答5:
You could test if the command returns a string, the command substitution will capture the output:
[[ "$(rpm -qa | grep glib)" ]] && do something
来源:https://stackoverflow.com/questions/8084142/check-if-rpm-exists-in-bash-script-silently