Check for revs in bash script mode

I am trying to do a quick check to make sure rpm is installed in a bash script using an if statement. But I want to do it quietly. Currently, when I run the script and rpm exists, it displays the output of rpm on a screen that I don't want.

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

Maybe there is an option for rpm that I am missing? or do I just need to change my expression?

thanks

+4
source share
5 answers

1) You can add -q to grep

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

2) You can redirect the output of stout and / or stderr to / dev / null

 if rpm -qa | grep glib 2>&1 > /dev/null; then do something fi 
+5
source

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

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

So you can probably use this:

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

This path should be faster because it does not need to wait for the -qa (request of all) rpm packages to be installed, but simply asks for the target rpm package. Of course, you must know the correct name of the package you want to test if it is installed or not.

Note: Using RPM version 4.9.1.2 on Fedora 15

+9
source

You only need the -q option:

 $ 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 this:

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

You can 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 
+1
source

You can check if the command will return a string, command substitution will capture the output:

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

Source: https://habr.com/ru/post/1380629/


All Articles