External command runcher.
Input cmd_params into Kernel#spawn.
Write command’s stdout and stderr to tmp file.
* If Platform is "mswin" or "mingw" , then temp directory is ENV["TEMP"] * If Platform is "linux" or "cygwin" , then temp directory is "/tmp/"
Return hash object including stdout, stderr, and exit status.
require 'rbatch' cmd = RBatch::Cmd("ls") r = cmd.run p r.stdout => "fileA\nfileB\n"
cmd = RBatch::Cmd("ls", {:verbose => true}) r = cmd.run
require 'rbatch' r = RBatch::cmd("ls") p r.stdout => "fileA\nfileB\n"
Cmd instance
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 44: def initialize(cmd_str,opt = nil) 45: raise(CmdException,"Command string is nil") if cmd_str.nil? 46: @cmd_str = cmd_str 47: # parse option 48: @opt = @@def_opt.clone 49: @@def_opt.each_key do |key| 50: if opt != nil && opt[key] != nil 51: # use argument 52: @opt[key] = opt[key] 53: elsif RBatch.common_config != nil && RBatch.common_config["cmd_" + key.to_s] != nil 54: # use config 55: @opt[key] = RBatch.common_config["cmd_" + key.to_s] 56: else 57: # use default 58: end 59: end 60: end
Run command
instance of RBatch::CmdResult
# File lib/rbatch/cmd.rb, line 67 67: def run() 68: stdout_file = Tempfile::new("rbatch_tmpout",RBatch::tmp_dir) 69: stderr_file = Tempfile::new("rbatch_tmperr",RBatch::tmp_dir) 70: pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"]) 71: status = Process.waitpid2(pid)[1] >> 8 72: result = RBatch::CmdResult.new(stdout_file,stderr_file,status,@cmd_str) 73: if @opt[:raise] && status != 0 74: raise(CmdException,"Command exit status is not 0. result: " + result.to_s) 75: end 76: return result 77: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.