Running a command from Ruby displaying and capturing the output

前端 未结 4 1727
情书的邮戳
情书的邮戳 2020-12-01 03:33

Is there some way to run a (shell) command from Ruby displaying but also capturing the output? Maybe with the help of some gem?

What I mean by displaying is not prin

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 04:08

    If you are willing to explore a solution outside the standard library, you may also use Mixlib::ShellOut to both stream output and capture it:

    require 'mixlib/shellout'
    cmd = 'while true; do date; sleep 2; done'
    so = Mixlib::ShellOut.new(cmd)
    so.live_stream = $stdout
    so.run_command
    out = so.stdout
    

提交回复
热议问题