tags:

views:

47

answers:

2

I'm doing something like this:

$printer = %x(lpstat -p | grep -q "Eileen" && echo "true" || echo "nil").chomp

if $printer == "true"
  puts "do something here"
else
  puts "do something else"
end

Is there an easier/shorter way to do that? I'm just checking to see if a print queue exists, and needing to do one thing if it does and another if it doesn't. Thanks!

+4  A: 
if system 'lpstat -p | grep -q "Eileen"'
  puts "do something here"
else
  puts "do something else"
end

However this will print the output of the command to stdout. If you want to avoid that, you can do:

output = %x(lpstat -p | grep -q "Eileen")
if $?.success?
  puts "do something here"
else
  puts "do something else"
end
sepp2k
+3  A: 

You can test the output if lpstat -p in the ruby script.

printer = %x(lpstat -p)

if printer =~ /Eileen/
  puts "do something here"
else
  puts "do something else"
end

PS: you might not need to use a global here - or even a variable:

  if %x(lpstat -p) =~ /Eileen/
philippe
This worked exactly how I needed it. Thanks!