"Eivind Eklund" <eeklund / gmail.com> schrieb im Newsbeitrag
news:dc9b20bb04100112302938e85f / mail.gmail.com...
> On Sat, 2 Oct 2004 00:09:59 +0900, Robert Klemme <bob.news / gmx.net>
wrote:
> >
> > "Eivind Eklund" <eeklund / gmail.com> 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.
>
> I'm not thinking of IRB  - I'm thinking of scripting specifically.
>
> > >  Ie, including
> > > pipes etc.
> >
> > IO.popen() uses pipes internally.  And I think there is also another
> > method.
>
> Yes, but these are highly clumsy when doing complex operations.
>
> > >  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().
>
> I know, but again: This does not handle the cases I'm interested in.
>
> > > 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 don't want all Ruby programs to look like shell scripts.  However, I
> find the shell script syntax very convenient for some types of
> operations - more convenient than Ruby for programs where the chief
> operations are running external commands with pipes and filters.  I
> would like to be able to embed this into Ruby programs, and easily
> convert this kind of program from a shell script to Ruby for further
> massaging.
>
> So yes, I really want an sh-like syntax inside my Ruby scripts - not
> for everything, but for the cases where I presently end up using sh
> because Ruby is inconvenient.

I'm not sure whether it's possible to reach the level of conformance that
you're aiming at.  I personally have no problem to use shell scripts for
certain tasks and ruby scripts for others.  Maybe it turns out that ruby
is just not the appropriate tool for the task.

> > 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.
>
> Intriguing idea.  I'd use an array per command instead of a string (to
> avoid white space parsing issues), but apart from that I like it :-)
>
> It isn't quite what I'm thinking of, but it is more useful for the
> general case.  (I was thinking of something for very special cases,
> only usable inside particular blocks.)

Well, or a combination as with system today:

IO.pipe( "ls -laF", %w{grep -v bar}, ["wc", "-l"]) do |io|
  while (l=io.gets)
    puts l
  end
end

Kind regards

    robert