Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

execute a command using OPEN

by kaka_2 (Sexton)
on Sep 10, 2014 at 14:01 UTC ( [id://1100145]=perlquestion: print w/replies, xml ) Need Help??

kaka_2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I want to execute a command using open which is using "|" inside it. Sample command is "ls -ltr /tmp | wc -l".

i wrote the simple codes as below.

#!/usr/bin/perl use warnings; my $cmd = "ls -ltr | wc -l"; open (CCCC, "$cmd |") || die "Failed: $! \n"; close CCCC;

and now i get a exception "Broken pipe". Can someone help me with it.

Replies are listed 'Best First'.
Re: execute a command using OPEN
by zentara (Archbishop) on Sep 10, 2014 at 14:48 UTC
    I don't get your broken pipe error, I get nothing. What I think you are failing to do is actually read the CCCC filehandle.

    This works:

    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; my $pid = open3(0, \*READ, 0,"ls -ltr | wc -l"); #if \*ERROR is false, STDERR is sent to STDOUT #get the answer chomp(my $answer = <READ>); print "query = $answer\n"; waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: execute a command using OPEN
by ikegami (Patriarch) on Sep 10, 2014 at 15:25 UTC

    That error originates from the shell as a result of wc being killed by SIGPIPE when it tries to write to its output handle after you closed it in your program.

    If you were to actually read wc's output, you wouldn't have this problem.

Re: execute a command using OPEN (broken pipe)
by tye (Sage) on Sep 10, 2014 at 14:48 UTC

    Read from the CCCC handle that you opened (to the end of the stream).

    - tye        

Re: execute a command using OPEN
by parv (Parson) on Sep 10, 2014 at 14:44 UTC

    None such problem here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100145]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found