Andy Res
Andy Res

Reputation: 16043

Show every line of output of an external command in real time

I found that you can run an external command from ruby, like this:

command = "find /home/user/workspace -name *.java"
%x(#{command})

and it works nice for commands that don't take too much time to execute, but for commands like the one above, which takes more time and progressively outputs the result, there's no way I can see the results until the command completes.

What I would like is to have the same look and feel as when the command is run directly from shell, in this particular case, as soon as a file is found, to show it on console.
Is this possible?

Upvotes: 0

Views: 63

Answers (1)

Amadan
Amadan

Reputation: 198324

Use IO.popen or Open3.

IO.popen("echo 1; sleep 1; echo 2; sleep 1; echo 3") do |io|
  io.each_line do |line|
    puts line
  end
end

Upvotes: 1

Related Questions