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
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 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
Run command
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
Generated with the Darkfish Rdoc Generator 2.