/ Published in: Ruby
                    
                                        
This script is intended to use after the pipe '|' on unix shell. It execute a one-line ruby code(you can split multi-line ruby-code with ';', of course) iteratively on each-line from the STDIN, each-line of which is assigned to a variable x.
eg.
ls | eachline.rb '$no||=1;puts "#$no - #{x}";$no+=1'
the output looks like this:
1 - README.image
2 - README.image~
3 - bin
4 - boot
...
Simple enough, isn't it?
                eg.
ls | eachline.rb '$no||=1;puts "#$no - #{x}";$no+=1'
the output looks like this:
1 - README.image
2 - README.image~
3 - bin
4 - boot
...
Simple enough, isn't it?
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
#!/usr/bin/env ruby
#supress any warning when calling other shell commands.
$VERBOSE = nil
def die(msg)
puts msg
exit 1
end
$cmd = ARGV.shift
die "you should specify a string of ruby code to deal with eachline of STDIN" unless $cmd
$in = STDIN.readlines
$in.each do |x|
x.chomp!
eval($cmd)
end
Comments
 Subscribe to comments
                    Subscribe to comments
                
                