简体   繁体   English

检查是否使用ruby脚本安装了rpm

[英]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 我正在尝试重写一些bash脚本,并且其中一个部分使用基本的if语句检查系统上是否安装了某些rpm

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. 我想在ruby中做类似的事情,但是对此还很陌生,并且不确定在文档中的位置。

Thanks 谢谢

You can invoke shell command in ruby script, and save output in variable 您可以在ruby脚本中调用shell命令,并将输出保存在变量中

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

You can do something like this in ruby code: 您可以在ruby代码中执行以下操作:

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. 系统调用将返回true或false取决于系统命令是否成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM