# \[Ruby Newbie\] Reading in a file generated by df

**URL:** <https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442>\
**Category:** Ruby\
**Created:** [November 9, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442 "2006-11-09T23:02:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmcilwain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac91a4/32.png) [@lmcilwain](https://www.ruby-forum.com/u/lmcilwain)\
**Post date:** [November 9, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442/1 "2006-11-09T23:02:03Z")

</div>

Hello all, (yes its me again 🙂 )

I’m here at work and was hit with a very interesting problem. My boss  
would like for me to build a script that monitors disk usage on our  
database systems (OS X).

I figured what better way to learn ruby then go right into a live  
situation. Granted, I’m not sure I’m ready for it but hell I will give  
it a shot anyway.

So I don’t have a specific way to implement this so I figure I try  
something that I believe to be basic.

1. Generate a file (df -k \> filename.txt)
2. Read the file using Ruby code  
A. compare bites or percentage size (which ever is easier) with a set  
size that I determine. If the size read in exceeds the size that I have  
determined then it shoots a warning to me via e mail letting me know I  
need to deal with it.

So, now that I have my idea out of my head an on paper, I started  
writing my code.

require ‘yaml’ # Just in case I need it  
doc\_array = [] # Array to have each line read from the file put into a  
list

file = File.new(‘diskcheck.txt’,‘r’) # Read the file  
while(line = file.gets) # Condition to put the files into the array  
doc\_array[doc\_array.length] = line  
end

puts doc\_array # Test to see if file was read into array correctly

So on to my problem (thanks for reading this far),  
When I try to get an ouput of the array that is supposed to have the  
several lines of the file read, it actually doesn’t come up with  
anything.

I run my program and it just exits without any errors but there is also  
no output that comes to the screen… Do I need to convert this file or  
find a better way to read in this file?

When I read in this file, It doesn’t spit out anything

---

<div class="post-metadata">

**Author:** ![lmcilwain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac91a4/32.png) [@lmcilwain](https://www.ruby-forum.com/u/lmcilwain)\
**Post date:** [November 9, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442/2 "2006-11-09T23:46:23Z")

</div>

I’d say your code is correct. I tried it myself, and it works. I use  
linux, though, but I don’t think this makes a difference. The only  
reason I can think of for what happens to you is that the file you’re  
trying to read is empty, but you’ll have already checked that. At any  
rate, there’s a far simpler way to read the contents of the file:

doc\_array=File.readlines(‘diskcheck.txt’)

I hope this can help

---

<div class="post-metadata">

**Author:** ![lmcilwain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac91a4/32.png) [@lmcilwain](https://www.ruby-forum.com/u/lmcilwain)\
**Post date:** [November 10, 2006, 6:51am UTC](https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442/3 "2006-11-10T06:51:22Z")

</div>

> 1. Generate a file (df -k \> filename.txt)

Your logic is correct, but if a human being is monitoring that disk  
space,  
it would be better to use df -kh so you get ``easily interpretable"  
values,  
the `h’ for human readable form. This is a well know fact, but i just  
thought i’d mention it anyway. 🙂

Regards,

- vihan

---

<div class="post-metadata">

**Author:** ![lmcilwain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac91a4/32.png) [@lmcilwain](https://www.ruby-forum.com/u/lmcilwain)\
**Post date:** [November 10, 2006, 8:37am UTC](https://www.ruby-forum.com/t/ruby-newbie-reading-in-a-file-generated-by-df/75442/4 "2006-11-10T08:37:29Z")

</div>

Why create the file at all?

disk\_check = `df -k` # shell out for the info directly  
disk\_check\_lines = disk\_check.split("/\n") # break it up  
disk\_check\_lines.each { test } #do your real test

William
