FAQ
Hi,
Is there any way to run a shell command from within a perl script and
capture the output in, say, an array?
One can run a shell command through the system function. But system itself
just returns a status code.

--
===================================
Anjan Purkayastha, PhD
Senior Computational Biologist
TessArae LLC
46090 Lake Center Plaza, Suite 304
Potomac Falls, VA 20165**
Office- 703.444.7188 ext. 116
Mobile-703.740.6939
===================================

Search Discussions

  • Jins Thomas at Apr 5, 2011 at 1:30 pm

    On Tue, Apr 5, 2011 at 6:54 PM, ANJAN PURKAYASTHA wrote:

    Hi,
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system itself
    just returns a status code.

    --
    Is this ok?

    my @files =`ls`;
    print @files;


    ===================================
    Anjan Purkayastha, PhD
    Senior Computational Biologist
    TessArae LLC
    46090 Lake Center Plaza, Suite 304
    Potomac Falls, VA 20165**
    Office- 703.444.7188 ext. 116
    Mobile-703.740.6939
    ===================================
  • Sunita Rani Pradhan at Apr 5, 2011 at 1:32 pm
    Hi Anjan

    You can execute shell command inside "``" and capture the output in
    variable . e,g:
    --------------
    $, = " ";
    $ls = `ls`;
    print $ls;
    @ls = split(/\n/,$ls);
    print @ls;
    ------------------

    Thanks
    Sunita


    -----Original Message-----
    From: ANJAN PURKAYASTHA
    Sent: Tuesday, April 05, 2011 6:54 PM
    To: Perl Beginners
    Subject: Capturing the output of a shell command

    Hi,
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system
    itself
    just returns a status code.

    --
    ===================================
    Anjan Purkayastha, PhD
    Senior Computational Biologist
    TessArae LLC
    46090 Lake Center Plaza, Suite 304
    Potomac Falls, VA 20165**
    Office- 703.444.7188 ext. 116
    Mobile-703.740.6939
    ===================================
  • Shawn H Corey at Apr 5, 2011 at 1:49 pm

    On 11-04-05 09:24 AM, ANJAN PURKAYASTHA wrote:
    Hi,
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system itself
    just returns a status code.
    You could use open:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
    chomp( my @files = <$ls_fh> );
    close $ls_fh or die "could not open `ls`: $!\n";

    print "@files\n";
    __END__


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Confusion is the first step of understanding.

    Programming is as much about organization and communication
    as it is about coding.

    The secret to great software: Fail early & often.

    Eliminate software piracy: use only FLOSS.
  • ANJAN PURKAYASTHA at Apr 5, 2011 at 1:55 pm
    thank you all!
    Anjan
    On Tue, Apr 5, 2011 at 9:49 AM, Shawn H Corey wrote:
    On 11-04-05 09:24 AM, ANJAN PURKAYASTHA wrote:

    Hi,
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system itself
    just returns a status code.
    You could use open:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
    chomp( my @files = <$ls_fh> );
    close $ls_fh or die "could not open `ls`: $!\n";

    print "@files\n";
    __END__


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Confusion is the first step of understanding.

    Programming is as much about organization and communication
    as it is about coding.

    The secret to great software: Fail early & often.

    Eliminate software piracy: use only FLOSS.

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/


    --
    ===================================
    Anjan Purkayastha, PhD
    Senior Computational Biologist
    TessArae LLC
    46090 Lake Center Plaza, Suite 304
    Potomac Falls, VA 20165**
    Office- 703.444.7188 ext. 116
    Mobile-703.740.6939
    ===================================
  • Chap Harrison at Apr 5, 2011 at 2:11 pm

    On Apr 5, 2011, at 8:49 AM, Shawn H Corey wrote:

    You could use open:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
    chomp( my @files = <$ls_fh> );
    close $ls_fh or die "could not open `ls`: $!\n";

    print "@files\n";
    __END__
    Is there any reason not to just use qx?

    #!/usr/bin/env perl

    use strict;
    use warnings;

    chomp(my @files = qx(ls));
    print "@files\n";
    __END__
  • Rob Dixon at Apr 5, 2011 at 2:18 pm

    On 05/04/2011 15:11, Chap Harrison wrote:
    Is there any reason not to just use qx?

    #!/usr/bin/env perl

    use strict;
    use warnings;

    chomp(my @files = qx(ls));
    print "@files\n";
    Hi Chap

    Just being picky, this will print all of the output lines on a single
    line separated by spaces. Since you probably want to see them on
    separate lines, you could either not chomp

    my @files = qx(ls);
    print @files;

    or append newlines on output

    chomp(my @files = qx(ls));
    print "$_\n" foreach @files;

    Cheers,

    Rob
  • Shawn H Corey at Apr 5, 2011 at 2:23 pm

    On 11-04-05 10:11 AM, Chap Harrison wrote:
    Is there any reason not to just use qx?
    If you use open, any error is in $! which gives you somewhere to start
    looking if something goes wrong. If you use qx() or back-ticks, any
    error is in $? which is the child error. This is not an error message
    but a number which you have to decode and it might lead you to the problem.


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Confusion is the first step of understanding.

    Programming is as much about organization and communication
    as it is about coding.

    The secret to great software: Fail early & often.

    Eliminate software piracy: use only FLOSS.
  • Jim Gibson at Apr 5, 2011 at 3:23 pm
    On 4/5/11 Tue Apr 5, 2011 7:11 AM, "Chap Harrison" <clh@pobox.com>
    scribbled:
    On Apr 5, 2011, at 8:49 AM, Shawn H Corey wrote:

    You could use open:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
    chomp( my @files = <$ls_fh> );
    close $ls_fh or die "could not open `ls`: $!\n";

    print "@files\n";
    __END__
    Is there any reason not to just use qx?
    For short programs with small amounts of output qx and backticks are fine.
    For longer executing programs or those with lots of output, you might want
    to use open because that way you can get the output lines from the program
    as they are generated, rather than waiting until the program has completed.
  • John W. Krahn at Apr 6, 2011 at 3:53 am

    Shawn H Corey wrote:
    On 11-04-05 09:24 AM, ANJAN PURKAYASTHA wrote:
    Hi,
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system
    itself
    just returns a status code.
    You could use open:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
    chomp( my @files = <$ls_fh> );
    close $ls_fh or die "could not open `ls`: $!\n";
    Read:

    perldoc -f close

    To see the proper way to close a pipeed open.



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
  • Rob Dixon at Apr 5, 2011 at 2:13 pm

    On 05/04/2011 14:24, ANJAN PURKAYASTHA wrote:
    Is there any way to run a shell command from within a perl script and
    capture the output in, say, an array?
    One can run a shell command through the system function. But system itself
    just returns a status code.
    You should read the section on qx/STRING/ in perldoc perlop. Backticks
    `` are the same as qx//, but I prefer the latter as it is more visible,
    and backticks are liable to be confused with single quotes ''.

    If you use qx// in scalar context it will return everything the command
    sends to stdout in a single string. If you put it in list context it
    will return a list of lines, each terminated by the current records
    separator - probably "\n".

    HTH,

    Rob

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 5, '11 at 1:24p
activeApr 6, '11 at 3:53a
posts11
users8
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase