grab output shell

  • Thread starter Andrea Spitaleri
  • Start date
A

Andrea Spitaleri

Hi
how do I grab the output from shell after system?
I have a program which print out some information on the shell before
give you the prompt, something like that:

bash>program
so far so good...
version 2.1
help file program -h
program>

I would like to grab these lines before I get the prompt "program>"
and check for the versione. I tought that the below script could work
but it doesn't (that's normal because I am reading the output after
the system call):

#!/usr/bin/perl

use warnings;
use strict;


system "rasmol > out ";
open (OUT,"out") or die " $! ";
while(<OUT>){
chomp;
if (/^Version/) {
my @version=split;
if ($version[1]eq "2.7.2.1"){
exit;
}
}
}
I think that I need something more dynamic...:)
cheers

and
 
T

Tad McClellan

Andrea Spitaleri said:
how do I grab the output from shell after system?


You should read the documentation for the functions that you use.

If you had adopted such a common sense method, you wouldn't be here,
as the docs for system tell how to capture the output.


perldoc -f system
 
M

Marcus Stollsteimer

Tad said:
You should read the documentation for the functions that you use.

If you had adopted such a common sense method, you wouldn't be here,
as the docs for system tell how to capture the output.

Andrea Spitaleri said:
I would like to grab these lines before I get the prompt "program>"
and check for the versione.


I think the problem of the OP is, that the output should be grabbed
and processed while the program is still running.


Andrea,

I don't know how to do this, but this might be
a possible workaround:

has your 'program' a version option (like --version or -v)
that prints only the version and exits?
Then you could call it first to grab the output (qx// or "backticks")
and in case it's the right version call it again.

Regards,
Marcus
 
B

Ben Morrow

Quoth Marcus Stollsteimer said:
I think the problem of the OP is, that the output should be grabbed
and processed while the program is still running.

Andrea,

I don't know how to do this, but this might be
a possible workaround:

Check out Expect.pm.

Ben
 
J

Joe Smith

Andrea said:
how do I grab the output from shell after system?

You can't, without external help, as you have discovered.

For non-interactive programs where you can wait for the
program to finish first, grab the output from qx().

For processing the results as they come in,
open my $pipe, '-|', 'rasmol';

For interactive programs, let Expect.pm open a pty for you.
system "rasmol > out ";

If the program sends its usage message to STDERR (which is still
connected to your terminal), then it won't go to the 'out' file.

system "rasmol >stdout.txt 2>stderr.txt";
open my $prog_out,'<','stdout.txt' or warn();
open my $prog_err,'<','stderr.txt' or warn();

Most likely the info you want went to STDERR instead of STDOUT.

$usage_string = `rasmol 2>&1`;

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top