Run linux command in ruby

I'm trying to do the following..

   if "ps | grep -e file" then
      true;
   else
      false;

What would I need to do to make the string n the if statement execute the linux command listed?


Simply use system("command") . It'll return true if the command was successfully executed.

EDIT Just read your question again, and I believe that what you're looking for is this:

if `ps | grep -e file`.empty? # no matches
  true
else
  false
end

this has been asked here already: Calling shell commands from Ruby

The simple answer would be

if `ps | grep -e file` then
    true
else
    false
end

Don't have to call grep . Just call ps and let Ruby find the things you want.

if `ps`["file"]
  ...
else
  ...
end
链接地址: http://www.djcxy.com/p/25300.html

上一篇: 外部命令如何将其输出和退出状态返回给Ruby?

下一篇: 在ruby中运行linux命令