# Stdin stdout -ing my program

**URL:** <https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882>\
**Category:** Ruby\
**Created:** [August 12, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882 "2007-08-12T23:00:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 12, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/1 "2007-08-12T23:00:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 12, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/2 "2007-08-12T23:14:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 13, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/3 "2007-08-13T21:10:09Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 14, 2007, 5:00am UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/4 "2007-08-14T05:00:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 13, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/5 "2007-08-13T21:46:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [August 14, 2007, 5:23am UTC](https://www.ruby-forum.com/t/stdin-stdout-ing-my-program/111882/6 "2007-08-14T05:23:51Z")

</div>

On 14 Aug 2007, at 12:00, Jon H. wrote:

> Thanks a ton  
> -Jon
> 
> –  
> Posted via [http://www.ruby-forum.com/](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
