Derek Smith wrote:

> dfstr = Array.new
> dfstr << %x(df -m)
> dfstr.each do |line|
>     puts line if line =~ /(\d+)%/
> end
> 
> Since its one line, it of course prints it all.
> I then tried:
> 
> dfstr.each do |line|
>     puts line.split[3]
> end
> 
> And it only prints "Avail"

You want to split the output of 'df' into lines.

Let's start with this:

   dfstr = %x(df -m)

'dfstr' now contains the complete output, as one string. You could do 
'dfstr.split("\n")' to split it into lines but this isn't necessary, 
because a string already has an each_line iterator:

   dfstr.each_line { |line|
      puts "a line: #{line}"
   }

So your complete program should look like:

   dfstr = %x(df -m)
   dfstr.each_line { |line|
      puts line.split[3]
   }

Now, supose you want to calculate the sum of all the totals? It's easy. 
First, let's "replace" every line with its 4'th field:

   dfstr = `df -m`
   p dfstr.each_line.map { |ln| ln.split[3] }

And let's get rid of the "Available" line, and convert all strings to 
numbers:

   dfstr = `df -m | tail -n +2`
   p dfstr.each_line.map { |ln| ln.split[3].to_i }

And let's sum it up:

   dfstr = `df -m | tail -n +2`
   puts "Total available space:"
   puts dfstr.each_line.map { |ln| ln.split[3].to_i }.reduce { |sum, n| 
sum += n }

(BTW, You can do 'df -x tmpfs' to ommit temporary filesystems.)
-- 
Posted via http://www.ruby-forum.com/.