I want to process the output of a pipe actively. If I'd, for example, host a server with an interactive console, how would I take the output live and process it?
Asked
Active
Viewed 132 times
0
-
Over what, the console? A network connection? – tadman Nov 21 '17 at 21:15
-
I think something along the line `while line_in=STDIN.read do ... end` should do the job. Probably you can provide some test "code" (bash script) to explain better what you want to achieve. – Felix Nov 22 '17 at 11:15
-
Wait a second, you want to give the server input or process its output or both? Please improve your question and provide examples. – Felix Nov 22 '17 at 11:34
1 Answers
1
You'll find some nice explanations in Best practices with STDIN in Ruby? .
You can do the following
#!/usr/bin/env ruby
ARGF.each_line do |line|
puts line.upcase
end
Given a script like
#!/bin/bash
echo "abcde"
sleep 2
echo "oiiausd"
sleep 2
and feeding them like
$ ./bash_script.sh | ./ruby_script.rb
will output
ABCDE
OIIAUSD
Note however that you'll might have to deal with IO buffering and stuff if you notice that processing of the input stream does not work as expected.
Also note that usage of ARGF-magic has the advantage that you could also consume a given log-file like ./ruby_script.rb LOGFILE.log
(without the pipe).

Felix
- 4,510
- 2
- 31
- 46