Evaluate% x () to True or False in Ruby

I am 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 this? I just check if there is a print queue, and one thing needs to be done if this happens, and the other if it is not. Thank!

+3
source share
2 answers

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

printer = %x(lpstat -p)

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

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

  if %x(lpstat -p) =~ /Eileen/
+4
source
if system 'lpstat -p | grep -q "Eileen"'
  puts "do something here"
else
  puts "do something else"
end

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

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

Source: https://habr.com/ru/post/1751761/


All Articles