From: Robert Klemme Date: 2010-11-28T21:00:15+09:00 Subject: Re: How to use Ruby like shell script? On 28.11.2010 09:50, Yu-Hsuan Lai wrote: > OH, sorry for my poor English. > Infact what I want to do is not "use ruby like shell script" but "use shell > in ruby". > For instant, I wonder if I can use "ls | grep" or "tar -xvf | grep" to get a > file list and store it in a ruby array, and handle them with io#gets. You do not need to call "ls" from Ruby scripts because there are Dir[], Dir.glob and File.stat. In fact, this is much more efficient and robust (semantics of "ls" changes dramatically between operating systems). For the case with "tar" you can do # 1.9 IO.popen ["tar", "xvf", file_name] do |io| io.each do |line| puts "found #{line}" if /\.rb$/ =~ line end end # 1.8 IO.popen "tar xvf #{file_name}" do |io| io.each do |line| puts "found #{line}" if /\.rb$/ =~ line end end You can even use grep: io.grep /\.rb$/ do |match| puts "found: #{match}" end > I don't know what "back ticks" are. output=`ls -l` Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/