检查rpm是否安装了ruby脚本。

时间:2021-08-07 19:23:52

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脚本,其中一节检查系统上是否安装了特定的rpm,并使用基本的if语句

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

谢谢

2 个解决方案

#1


3  

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

#2


3  

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,具体取决于系统命令是否成功。

#1


3  

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

#2


3  

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,具体取决于系统命令是否成功。