Parent

Methods

RBatch::Cmd

External command runcher.

Sample 1

require 'rbatch'
cmd = RBatch::Cmd("ls")
r = cmd.run
p r.stdout
=> "fileA\nfileB\n"

Sample 2 ( Use option)

cmd = RBatch::Cmd("ls", {:verbose => true})
r = cmd.run

Sample 3 ( Use alias)

require 'rbatch'
r = RBatch::cmd("ls")
p r.stdout
=> "fileA\nfileB\n"

Public Class Methods

new(cmd_str,opt = nil) click to toggle source

Cmd instance

Params

cmd_str = Command string. Such ad "ls -l" opt = Option hash object. Hash keys is follows.

  • :raise (Boolean) = If command exit status is not 0, raise exception. Default is false.

# File lib/rbatch/cmd.rb, line 44
def initialize(cmd_str,opt = nil)
  raise(CmdException,"Command string is nil") if cmd_str.nil?
  @cmd_str = cmd_str
  # parse option
  @opt = @@def_opt.clone
  @@def_opt.each_key do |key|
    if opt != nil  && opt[key] != nil
      # use argument
      @opt[key] = opt[key]
    elsif RBatch.common_config != nil            && RBatch.common_config["cmd_" + key.to_s] != nil
      # use config
      @opt[key] = RBatch.common_config["cmd_" + key.to_s]
    else
      # use default
    end
  end
end

Public Instance Methods

run() click to toggle source

Run command

Return

instance of RBatch::CmdResult

# File lib/rbatch/cmd.rb, line 67
def run()
  stdout_file = Tempfile::new("rbatch_tmpout",RBatch::tmp_dir)
  stderr_file = Tempfile::new("rbatch_tmperr",RBatch::tmp_dir)
  pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"])
  status =  Process.waitpid2(pid)[1] >> 8
  result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str)
  if @opt[:raise] && status != 0
    raise(CmdException,"Command exit status is not 0. result: " + result.to_s)
  end
  return result
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.