class SysCmd::Command

An executable system command

Attributes

command[R]
error[R]
error_output[R]
input[R]
output[R]
status[R]

Public Class Methods

new(command, options = {}) click to toggle source
# File lib/sys_cmd.rb, line 207
def initialize(command, options = {})
  if command.respond_to?(:shell)
    @command = command.command
    @shell = command.shell
    @input = command.stdin_data
  else
    @command = command
    @shell = Shell.new(options)
    @input = options[:stdin_data]
  end
  @output = nil
  @status = nil
  @error_output = nil
  @error = nil
end

Public Instance Methods

error?() click to toggle source

did the command execution caused an exception?

# File lib/sys_cmd.rb, line 311
def error?
  !@error.nil?
end
run(options = {}) click to toggle source

Execute the command.

By default the command is executed by a shell. In this case, unquoted arguments are interpreted by the shell, e.g.

SysCmd.command('echo $BASH').run # /bin/bash

When the :direct option is set to true, no shell is used and the command is directly executed; in this case unquoted arguments are not interpreted:

SysCmd.command('echo $BASH').run # $BASH

The exit status of the command is retained in the status attribute (and its numeric value in the status_value attribute).

The standard output of the command is captured and retained in the output attribute.

By default, the standar error output of the command is not captured, so it will be shown on the console unless redirected.

Standard error can be captured and interleaved with the standard output passing the option

error_output: :mix

Error output can be captured and keep separate inthe error_output attribute with this option:

error_output: :separate

The value returned is by defaut, like in Kernel#system, true if the command gives zero exit status, false for non zero exit status, and nil if command execution fails.

The :return option can be used to make this method return other attribute of the executed command.

The :stdin_data option can be used to pass a String as the command's standar input.

# File lib/sys_cmd.rb, line 267
def run(options = {})
  @output = @status = @error_output = @error = nil
  if options[:direct]
    command = @shell.split(@command)
  else
    command = [@command]
  end
  stdin_data = options[:stdin_data] || @input
  if stdin_data
    command << { stdin_data: stdin_data }
  end
  begin
    case options[:error_output]
    when :mix # mix stderr with stdout
      @output, @status = Open3.capture2e(*command)
    when :separate
      @output, @error_output, @status = Open3.capture3(*command)
    else # :console (do not capture stderr output)
      @output, @status = Open3.capture2(*command)
    end
  rescue => error
    @error = error.dup
  end
  case options[:return]
  when :status
    @status
  when :status_value
    status_value
  when :output
    @output
  when :error_output
    @error_output
  when :command
    self
  else
    @error ? nil : @status.success? ? true : false
  end
end
status_value() click to toggle source
# File lib/sys_cmd.rb, line 306
def status_value
  @status && @status.exitstatus
end
success?() click to toggle source

did the command execute without error and returned a success status?

# File lib/sys_cmd.rb, line 316
def success?
  !error? && @status.success?
end
to_s(options = {}) click to toggle source
# File lib/sys_cmd.rb, line 320
def to_s(options = {})
  if @input && options[:with_input]
    "#{command}#{@shell.here_doc(@input)}"
  else
    command
  end
end