i need to be able to run my program fully from the command line, instead
of ‘ruby program.rb’…i need to pipe the commands using standard input
and output.
How would i do this with the code below?
Code:
require ‘find’
Dir.chdir("/")
res = []
Find.find("/") {|path| res << File.size( path).to_f if File.file?(
path)}
puts “Total Number of Files: #{res.length}”
puts “Average File Size: #{res.inject(0) {|sum, i| sum +
i}/res.length/1024} Kilobytes”
puts “Max File Size: #{res.max/1024}KB”
puts “Min File Size: #{res.min}KB”
puts “================================”
puts “================================”
res.delete_if {|b| b < 1}
puts “Weighted Average: #{res.inject(0) {|sum, i| sum +
i}/res.length/1024} Kilobytes”
Lol Standard Deviation
def mean(res)
res.inject(0) {|sum, x| sum += x} / res.size.to_f / 1024
end
def mean_and_standard_deviation(res)
m = mean(res)
variance = res.inject(0) {|variance, x| variance += (x - m) ** 2}
Math.sqrt(variance/(res.size - 1)) / 1024
end
puts “Standard Deviation = #{mean_and_standard_deviation(res)}”
##############
So how would i pipe find from a shell command?
Thanks,
-Jon
On Mon, Aug 13, 2007 at 06:00:13AM +0900, Jon H. wrote:
Find.find("/") {|path| res << File.size( path).to_f if File.file?(
i}/res.length/1024} Kilobytes"
end
puts “Standard Deviation = #{mean_and_standard_deviation(res)}”
##############
So how would i pipe find from a shell command?
Thanks,
I’m not quite sure if I know what your question is. What do you want to
pipe into or out of your ruby program. Do you want to do something
like:
find /some/path -type f | ruby program.rb
and have ruby calculate sizes on your files and output summary
information?
If that is the case then the file names appear on stdin of the ruby
program and you can do:
$stdin.each_line do |file_path|
res << File.size(file_path)
end
Is that what you are looking for?
enjoy,
-jeremy
like:
find /some/path -type f | ruby program.rb
and have ruby calculate sizes on your files and output summary
information?
If that is the case then the file names appear on stdin of the ruby
program and you can do:
$stdin.each_line do |file_path|
res << File.size(file_path)
end
Is that what you are looking for?
enjoy,
-jeremy
#!/usr/bin/ruby
require ‘find’
Dir.chdir("/")
res = []
$stdin.each_line do |file_path|
res << File.size(file_path)
end
puts “Total Number of Files: #{res.length}”
after trying the find /etc/ -type f | ruby program.rb
i get the following error that i cant seem to figure out
v2count.rb:6:in size': No such file or directory - /etc/fstab (Errno::ENOENT) from v2count.rb:6 from v2count.rb:5:in
each_line’
from v2count.rb:5
Thanks
-Jon
Worked like a charm, but i have yet one more question for you
if i wanted to run it like this:
find /etc/ -type f | ./program.rb
how would i get it to the ./program.rb state
friend gave me the clue of the numbers 699 - 756.
I have no idea what that relates to.
Thanks a ton
-Jon
On Tue, Aug 14, 2007 at 04:10:18AM +0900, Jon H. wrote:
$stdin.each_line do |file_path|
res << File.size(file_path)
end
Is that what you are looking for?
enjoy,
-jeremy
My mistake, file_path contains the “\n”
#!/usr/bin/ruby
require ‘find’
Dir.chdir("/")
res = []
$stdin.each_line do |file_path|
res << File.size(file_path)
res << File.size(file_path.strip)
end
puts “Total Number of Files: #{res.length}”
after trying the find /etc/ -type f | ruby program.rb
i get the following error that i cant seem to figure out
% find /etc/ -type f 2> /dev/null | ruby jon-h.rb
Total Number of Files: 232
enjoy,
-jeremy
On 14 Aug 2007, at 12:00, Jon H. wrote:
Thanks a ton
-Jon
–
Posted via http://www.ruby-forum.com/.
Make program.rb executable and set the shebang line correctly? E.g:
[alexg@powerbook]/Users/alexg(7): cat hi.rb
#!/usr/local/bin/ruby
puts ‘hi’
[alexg@powerbook]/Users/alexg(8): ruby hi.rb
hi
[alexg@powerbook]/Users/alexg(9): ./hi.rb
tcsh: ./hi.rb: Permission denied.
[alexg@powerbook]/Users/alexg(10): chmod u+x hi.rb
[alexg@powerbook]/Users/alexg(11): ./hi.rb
hi
Not sure about your friends hint. At first I thought they were the
octal chmod strings for making files executable, but unless I’m
missing something they don’t seem to be…
Alex G.
Bioinformatics Center
Kyoto University