Danny
Danny

Reputation: 5400

Check if rpm is installed with ruby script

I'm trying to rewrite some bash scripts and one of the sections checks if certain rpm's are installed on the system with a basic if statement

if rpm -qa | grep rpmnamehere; then
     do stuff

I want to do something similar in ruby, but am pretty new to this and not sure where to look in the documentation.

Thanks

Upvotes: 2

Views: 2928

Answers (2)

DrChanimal
DrChanimal

Reputation: 681

You can do something like this in ruby code:

if system "rpm -qa | grep rpmnamehere"
   #additional ruby statements
end

the system call will return true or false depend if the system command is successful or not.

Upvotes: 3

evfwcqcg
evfwcqcg

Reputation: 16335

You can invoke shell command in ruby script, and save output in variable

a = %x{rpm -qa | grep rpmnamehere}
puts a

or only invoke command

`rpm -qa | grep rpmnamehere`

so, I think you can solve your problem like this

unless `rpm -qa | grep rpmnamehere`.empty?
  # do stuff
end

Upvotes: 3

Related Questions