From: Harry Ohlsen Date: 2003-10-03T10:54:33+09:00 Subject: Re: backquotes, system, shell and ruby John Carter wrote: > a = `wc *.c` > > What did that do? Same as open("|wc *.c"). > > It ran bash! > > Yuck! > > system( "tar -xvzf foo.tgz foo") > > What did that do? It ran shell! > > Why? Why does a scripting language like Ruby keep invoking an old and bad > scripting language call shell? I think it sits in the "it's the easiest thing to to do" basket. > Consider the "rename" facility that comes with perl these days. > For example, to rename all files matching "*.bak" to strip the exten- > sion, you might say > > rename 's/\.bak$//' *.bak > > To translate uppercase names to lower, you'd use > > rename 'y/A-Z/a-z/' * Something like that would be quite nice, since it's a pain in the butt to do with most shells ... strangely, this is one of the few things that DOS has done nicely for about ten years. Of course, it's pretty easy to do in Ruby already (warning: totally untested) ... Dir["*.bak"].each do |file| File.rename file, file.sub(/\.bak$/, "") end or maybe def rename(wildcard, &block) Dir[wildcard].each do |file| File.rename file, (yield file) end end rename("*.bak") {|f| f.sub(/\.bak$/, "")} > How hard would it be to extend the backtick syntax to be a pure ruby > thing. ie. So proc_exec doesn't invoke bash, but invokes Shell.rb? I guess it comes down to how much of the shell syntax you want to parse. While it's not hideously complex, sh/bash syntax is quite a funny grammar. I guess handling just globbing, pipes and redirection would be enough to make this useful, though. The other issue, I would imagine, is *which* shell's syntax to use. I've never needed to know, so I've not looked, but I assume both system() and backticks just end up calling the C system() library routine, which means that the shell invoked is effectively user-defined. Cheers, Harry O.