Validating a bash command in an if statement in Ruby

How to check the return value (true / false) of a bash command in an if expression in Ruby. I want something like this to work,

if ("/usr/bin/fs wscell > /dev/null 2>&1")
                has_afs = "true"
        else
                has_afs = "false"
        end

He complains about the following error value, it always returns true.

 (irb):5: warning: string literal in condition

What is the correct syntax?

UPDATE:

 /usr/bin/fs wscell 

Searches for an afsestablished and running condition. It will call such a line,

 This workstation belongs to cell <afs_server_name>

If it afsdoes not work, the team exits with status 1

+5
source share
2 answers

You need back quotes, not double quotes. To check the output of a program:

has_afs = `/usr/bin/fs wscell > /dev/null 2>&1` == SOMETHING ? 'true' : 'false'

Where SOMETHING is filled with what you are looking for.

+6
source

, system() Backticks , ($?. exitstatus):

: http://rubyquicktips.com/post/5862861056/execute-shell-commands)

UPDATE:

system("/usr/bin/fs wscell > /dev/null 2>&1")  # Returns false if command failed
has_afs = $?.exitstatus != 1  # Check if afs is running
+4

All Articles