From: Robert Klemme Date: 2004-10-02T00:09:59+09:00 Subject: Re: Ruby as a shell (was Re: irb: a great tool in it's own right) "Eivind Eklund" schrieb im Newsbeitrag news:dc9b20bb041001065332408f65@mail.gmail.com... > On the subject (if not the Subject:): Have anybody played with make it > possible to use Ruby for running sort-of shell scripts? This topic arises from time to time. AFAIR there are IRB extensions that make IRB shell-like. > Ie, including > pipes etc. IO.popen() uses pipes internally. And I think there is also another method. > As it is, there are some programs that call a lot of shell > commands that I write in sh instead of Ruby, and where I'd really like > to do them in Ruby. Depending on what you do, you might notice that you don't need those shell programs for the job. For example, "cat" is easily done with File.open() {|io|...} and "rm" can be taken care of by File.delete(). > The starting point I see for something like this would be to override > method_missing like above, but make it return an object where | is > overridden, and that have handling of shell return codes. > > The challenges I see here is > - It's not possible to override the trueness/falseness of an object, > so we'd need to call a special method to find out if a command failed > or passed > - It is hard to implement -e (give an error if a complete shell line > fails - but not if we have an fail || true). Maybe trace_func could > be of help here, though. I'm not sure whether I wanted Ruby scripts to look similar to shell scripts or whether it's feasible to a degree that is satisfactory. I wouldn't mind having a method pipe(*commands) in Kernel or IO that starts all these commands in parallel and connects them via pipes. I imagine usage like this: IO.pipe( "ls -laf", "grep -v foo" ) do |io| # io like in IO#popen while ( line = io.gets ) puts line end end Hm, this could even be an extension of IO#popen... Input of the IO instance would then be connected to the output of the last command and output would be connected to the input of the first command. That way one could send data around through the pipe. Funny idea. Kind regards robert