Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Shell pipeline in Ruby?

Reply
Thread Tools

Shell pipeline in Ruby?

 
 
Michal Suchanek
Guest
Posts: n/a
 
      05-12-2011
Hello,

how do you write an equivalent of

$ cmdA | cmdB | cmdC

in Ruby?

Specifically, I would like to see the PID, return value and stderr of
each of these commands but I would like cmdB to read the stdout of
cmdA directly, on its stdin, and similarily for cmdC and cmdB.

Here I am not interested in feeding cmdA some particular input (eg. it
can read /dev/null for all I care) but in general gluing arbitrary fd
to its stdin might be desirable in other cases.

Thanks

Michal

 
Reply With Quote
 
 
 
 
Jos Backus
Guest
Posts: n/a
 
      05-12-2011
[Note: parts of this message were removed to make it a legal post.]

Have you checked out Open3.pipeline?

Jos

--
Peace cannot be achieved through violence, it can only be attained through
understanding.
On May 12, 2011 9:06 AM, "Michal Suchanek" <> wrote:
> Hello,
>
> how do you write an equivalent of
>
> $ cmdA | cmdB | cmdC
>
> in Ruby?
>
> Specifically, I would like to see the PID, return value and stderr of
> each of these commands but I would like cmdB to read the stdout of
> cmdA directly, on its stdin, and similarily for cmdC and cmdB.
>
> Here I am not interested in feeding cmdA some particular input (eg. it
> can read /dev/null for all I care) but in general gluing arbitrary fd
> to its stdin might be desirable in other cases.
>
> Thanks
>
> Michal
>


 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      05-12-2011
Michal Suchanek wrote in post #998275:
> Hello,
>
> how do you write an equivalent of
>
> $ cmdA | cmdB | cmdC
>


How about:

puts `cmdA | cmdB | cmdC`

or

puts %x{cmdA | cmdB | cmdC}

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      05-12-2011
On 12 May 2011 19:22, Jos Backus <> wrote:
> Have you checked out Open3.pipeline?
>


I have checked all the stuff I could find once but did not find a solution.

That's why I am asking, perhaps I overlooked something.

While the pipeline gives the status of all the involved processes and
connects them properly which is more than I ever got from Ruby it is
not obvious how to capture the output - the stdout of the last
process can be captured with pipeline_r but the stderrs get sinked
somewhere in the pipeline_run.

Also the in and out options used in the example are not documented afaict.

Thanks

Michal

 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      05-12-2011
There is also popen4 which I believe also gives you the pid with std -
in/out/err

On Thu, May 12, 2011 at 2:41 PM, Michal Suchanek <> wrot=
e:
> On 12 May 2011 19:22, Jos Backus <> wrote:
>> Have you checked out Open3.pipeline?
>>

>
> I have checked all the stuff I could find once but did not find a solutio=

n.
>
> That's why I am asking, perhaps I overlooked something.
>
> While the pipeline gives the status of all the involved processes and
> connects them properly which is more than I ever got from Ruby it is
> not obvious how to capture the output =A0- the stdout of the last
> process can be captured with pipeline_r but the stderrs get sinked
> somewhere in the pipeline_run.
>
> Also the in and out options used in the example are not documented afaict=

 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      05-12-2011
On 12 May 2011 21:54, Stu <> wrote:
> There is also popen4 which I believe also gives you the pid with std -
> in/out/err
>


It provides stderr but not the pipeline which cannot be constructed in
pure ruby (unless a popen taking fd:s for stdio is implemented).

Thanks

Michal

 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      05-13-2011
In the spirit of the recent call for better ruby documentation, I wrote
a
writeup:

http://en.wikibooks.org/w/index.php?...ning_processes

Enjoy.
-roger-

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      05-13-2011
On 13 May 2011 05:13, Roger Pack <> wrote:
> In the spirit of the recent call for better ruby documentation, I wrote


Unfortunately, this does not work.

First, these pipelines don't always work as one would expect so
testing is *required* before posting a "solution".

Second, afaict the reopen method only changes the IOs at Ruby level,
not the fd:s at C level so it's useless in this context.

Thanks

Michal

> a
> writeup:
>
> http://en.wikibooks.org/w/index.php?...ning_processes
>
> Enjoy.
> -roger-
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-13-2011
On Thu, May 12, 2011 at 6:05 PM, Michal Suchanek <> wrote:
> how do you write an equivalent of
>
> $ cmdA | cmdB | cmdC
>
> in Ruby?
>
> Specifically, I would like to see the PID, return value and stderr of
> each of these commands but I would like cmdB to read the stdout of
> cmdA directly, on its stdin, and similarily for cmdC and cmdB.
>
> Here I am not interested in feeding cmdA some particular input (eg. it
> can read /dev/null for all I care) but in general gluing arbitrary fd
> to its stdin might be desirable in other cases.


$ ri Open3.pipeline_r Open3.pipeline_start

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Michal Suchanek
Guest
Posts: n/a
 
      05-13-2011
On 13 May 2011 10:44, Robert Klemme <> wrote:
> On Thu, May 12, 2011 at 6:05 PM, Michal Suchanek <> wrote:
>> how do you write an equivalent of
>>
>> $ cmdA | cmdB | cmdC
>>
>> in Ruby?
>>
>> Specifically, I would like to see the PID, return value and stderr of
>> each of these commands but I would like cmdB to read the stdout of
>> cmdA directly, on its stdin, and similarily for cmdC and cmdB.
>>
>> Here I am not interested in feeding cmdA some particular input (eg. it
>> can read /dev/null for all I care) but in general gluing arbitrary fd
>> to its stdin might be desirable in other cases.

>
> $ ri Open3.pipeline_r Open3.pipeline_start
>

Again, like in Open3.pipeline the :err argument used in the example is
not documented.

From the description of the example it seems it captures the stderr of
all the commands together.

Are there more arguments like that?

What do they mean?

Also this is available in ruby 1.9 only.

Thanks

Michal

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using subprocess module to launch a shell shell script that itselfforks a process Samuel A. Falvo II Python 0 10-08-2008 12:43 AM
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57