stdin stdout -ing my program

Discussion in 'Ruby' started by Jon Hawkins, Aug 12, 2007.

  1. Jon Hawkins

    Jon Hawkins Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Jon Hawkins, Aug 12, 2007
    #1
    1. Advertising

  2. On Mon, Aug 13, 2007 at 06:00:13AM +0900, Jon Hawkins wrote:
    > 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,


    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

    --
    ========================================================================
    Jeremy Hinegardner
    Jeremy Hinegardner, Aug 12, 2007
    #2
    1. Advertising

  3. Jon Hawkins

    Jon Hawkins Guest


    > 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
    --
    Posted via http://www.ruby-forum.com/.
    Jon Hawkins, Aug 13, 2007
    #3
  4. On Tue, Aug 14, 2007 at 04:10:18AM +0900, Jon Hawkins wrote:
    >
    > > 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


    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

    --
    ========================================================================
    Jeremy Hinegardner
    Jeremy Hinegardner, Aug 13, 2007
    #4
  5. Jon Hawkins

    Jon Hawkins Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
    Jon Hawkins, Aug 14, 2007
    #5
  6. On 14 Aug 2007, at 12:00, Jon Hawkins wrote:

    > 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
    >
    > --
    > 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 Gutteridge

    Bioinformatics Center
    Kyoto University
    Alex Gutteridge, Aug 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,773
    Chatoyer
    May 17, 2013
  2. Guest
    Replies:
    5
    Views:
    546
    Stuart MacMartin
    Aug 29, 2005
  3. Ben
    Replies:
    2
    Views:
    1,314
    jacob navia
    Aug 29, 2009
  4. Snail
    Replies:
    7
    Views:
    145
    Anno Siegel
    Apr 9, 2005
  5. Stefano Sabatini
    Replies:
    6
    Views:
    274
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page