當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Open3.pipeline_r用法及代碼示例


本文簡要介紹ruby語言中 Open3.pipeline_r 的用法。

用法

pipeline_r(*cmds, &block)

Open3.pipeline_r 將命令列表作為管道啟動,管道連接到最後一個命令的標準輸出。

Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
  ...
}

last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
...
last_stdout.close

每個 cmd 都是一個字符串或一個數組。如果它是一個數組,則將元素傳遞給 Process.spawn

cmd:
  commandline                              command line string which is passed to a shell
  [env, commandline, opts]                 command line string which is passed to a shell
  [env, cmdname, arg1, ..., opts]          command name and one or more arguments (no shell)
  [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)

Note that env and opts are optional, as for Process.spawn.

例子:

Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz",
                 [{"LANG"=>"C"}, "grep", "GET /favicon.ico"],
                 "logresolve") {|o, ts|
  o.each_line {|line|
    ...
  }
}

Open3.pipeline_r("yes", "head -10") {|o, ts|
  p o.read      #=> "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"
  p ts[0].value #=> #<Process::Status: pid 24910 SIGPIPE (signal 13)>
  p ts[1].value #=> #<Process::Status: pid 24913 exit 0>
}

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Open3.pipeline_r。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。