Can I get continuous output from system calls in Ruby? Can I get continuous output from system calls in Ruby? bash bash

Can I get continuous output from system calls in Ruby?


Try:

IO.popen("scp -v user@server:remoteFile /local/folder/").each do |fd|  puts(fd.readline)end


I think you would have better luck using the ruby standard library to handle SCP (as opposed to forking a shell process). The Net::SCP library (as well as the entire Net::* libraries) are full featured and used with Capistrano to handle remote commands.

Checkout http://net-ssh.rubyforge.org/ for a rundown of what is available.


Tokland answered the question as I asked it, but Adam's approach was what I ended up using. Here was my completed script, which does show a running count of bytes downloaded, and also a percentage complete.

require 'rubygems'require 'net/scp'puts "Fetching file"# Establish the SSH sessionssh = Net::SSH.start("IP Address", "username on server", :password => "user's password on server", :port => 12345)# Use that session to generate an SCP objectscp = ssh.scp# Download the file and run the code block each time a new chuck of data is receivedscp.download!("path/to/file/on/server/fileName", "/Users/me/Desktop/") do |ch, name, received, total|  # Calculate percentage complete and format as a two-digit percentage  percentage = format('%.2f', received.to_f / total.to_f * 100) + '%'  # Print on top of (replace) the same line in the terminal  # - Pad with spaces to make sure nothing remains from the previous output  # - Add a carriage return without a line feed so the line doesn't move down  print "Saving to #{name}: Received #{received} of #{total} bytes" + " (#{percentage})               \r"  # Print the output immediately - don't wait until the buffer fills up  STDOUT.flushendputs "Fetch complete!"