Capturing the output of a shell command

Capturing the output of a shell command

am 05.04.2011 15:24:07 von ANJAN PURKAYASTHA

--20cf307f367c7aa8c004a02bca8b
Content-Type: text/plain; charset=ISO-8859-1

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
===================================

--20cf307f367c7aa8c004a02bca8b--

Re: Capturing the output of a shell command

am 05.04.2011 15:30:23 von Jins Thomas

--e0cb4e3853c4df8b0f04a02be0fb
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Apr 5, 2011 at 6:54 PM, ANJAN PURKAYASTHA <
anjan.purkayastha@gmail.com> 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
> ===================================
>

--e0cb4e3853c4df8b0f04a02be0fb--

RE: Capturing the output of a shell command

am 05.04.2011 15:31:32 von Sunita Rani Pradhan

Hi Anjan

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

Thanks
Sunita


-----Original Message-----
From: ANJAN PURKAYASTHA [mailto:anjan.purkayastha@gmail.com]=20
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.

--=20
==================== =====3D=
==========
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
==================== =====3D=
==========

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

Re: Capturing the output of a shell command

am 05.04.2011 15:49:41 von Shawn H Corey

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/

Re: Capturing the output of a shell command

am 05.04.2011 15:55:47 von ANJAN PURKAYASTHA

--90e6ba6e8a4ab1fc8804a02c3b0e
Content-Type: text/plain; charset=ISO-8859-1

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
===================================

--90e6ba6e8a4ab1fc8804a02c3b0e--

Re: Capturing the output of a shell command

am 05.04.2011 16:11:24 von Chap Harrison

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

> You could use open:
>=20
> #!/usr/bin/env perl
>=20
> use strict;
> use warnings;
>=20
> open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
> chomp( my @files =3D <$ls_fh> );
> close $ls_fh or die "could not open `ls`: $!\n";
>=20
> print "@files\n";
> __END__

Is there any reason not to just use qx?

#!/usr/bin/env perl =
=20

use strict;
use warnings;

chomp(my @files =3D qx(ls));
print "@files\n";
__END__



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

Re: Capturing the output of a shell command

am 05.04.2011 16:13:36 von Rob Dixon

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


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

Re: Capturing the output of a shell command

am 05.04.2011 16:19:11 von Rob Dixon

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

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

Re: Capturing the output of a shell command

am 05.04.2011 16:23:38 von Shawn H Corey

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.

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

Re: Capturing the output of a shell command

am 05.04.2011 17:22:44 von Jim Gibson

On 4/5/11 Tue Apr 5, 2011 7:11 AM, "Chap Harrison"
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.




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

Re: Capturing the output of a shell command

am 06.04.2011 05:53:32 von jwkrahn

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

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