Tuesday, April 5, 2022

[SOLVED] Check if rpm exists in bash script silently

Issue

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) 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


Answered By - vromanov
Answer Checked By - Candace Johnson (WPSolving Volunteer)