1

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?

Skizit
  • 43,506
  • 91
  • 209
  • 269

4 Answers4

8

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
goncalossilva
  • 1,830
  • 15
  • 25
1

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

if `ps`["file"]
  ...
else
  ...
end
kurumi
  • 25,121
  • 5
  • 44
  • 52
1

Linux ps has a -C switch to check for a specific program name:

if "ps -C file" then
   true;
else
   false;

however - if you have a construct like

if (predicate) then true; else false; 

it should be the same as

(predicate) 
user unknown
  • 35,537
  • 11
  • 75
  • 121
0

You can run your Linux Command like this

`echo "done"`

This will print done as output.

Vijay
  • 443
  • 4
  • 13