Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Execute Command & Capture Output

1 view
Skip to first unread message

O. Olson

unread,
Jan 8, 2006, 8:19:08 PM1/8/06
to
Hi,
I am working on Linux, and would like perl to execute a program i.e.
executable and capture the output. From what I have read - it seems
that I should not use the system() command, but I should use the
backticks. Assuming the location of the executable is
/home/masca/org/10/exe

I have something like

open(OUTPUT, `\/home\/masca\/org\/10\/exe`);
while(<OUTPUT>) {
print;
}


But nothing happens. If I directly place that on the commandline - it
works fine.I hope someone has some ideas. I am sure this must be simple
- but I am new to perl.
Thanks a lot.
O.O.

use...@davidfilmer.com

unread,
Jan 8, 2006, 8:25:15 PM1/8/06
to
O. Olson wrote:
> open(OUTPUT, `\/home\/masca\/org\/10\/exe`);

You don't need to open it as a filehandle - just process the command
output directly, such as:

my $output = `\/home\/masca\/org\/10\/exe`; #scalar return
or my @output = `\/home\/masca\/org\/10\/exe`; #list return
or foreach my $output(`\/home\/masca\/org\/10\/exe`) {... #loop

Don't forget that if your command output includes newlines, you
(probably) should chomp() them off...

olso...@yahoo.it

unread,
Jan 8, 2006, 9:28:56 PM1/8/06
to
Dear Dave,
Thanks for your suggestions. I tried the scalar statement initially -
but actually my output is a list, so this did not work. I then tried

foreach my $output_1(`\/home\/masca\/org\/10\/exe`) {
#chomp();
print;
}
(Both with and without the chomp commented.) This does not seem to
work. I am not sure if my program is executed - but I just get a long
line of 222222222222's

I then tried
my @out_2 = `\/home\/masca\/org\/10\/exe`;
foreach(@out_2) {
#chomp();
print;
}
This does indeed work. (I think that the chomp() would come useful when
I process this.)

Another thing I noticed was that my backslashes were irrelevant at
least in the second case. (I can say this because the second case at
least works.) i.e.

my @out_2 = `/home/masca/org/10/exe`;
foreach(@out_2) {
#chomp();
print;
}

Seems to work just as fine.

Thanks a lot for your help and advice.

Regards,
O.O.

P.S.: - This really seems odd. I realized that the topics discussed in
this group were interesting to me, and I decided to subscribe to this
group, through google.

I was trying to post this message i.e. start a new topic - to this
group since last evening and it did not get through. I tried this
multiple times - and still nothing happened. In the end I unsubscribed,
and this post showed up within an hour or so. (However during this
time, I could reply to posts.)

use...@davidfilmer.com

unread,
Jan 9, 2006, 7:45:09 PM1/9/06
to
olso...@yahoo.it wrote:
> foreach my $output_1(`\/home\/masca\/org\/10\/exe`) {
> print;

> }
> This does not seem to work.

That's because you are assigning the value of each line in the output
to a specifically-named variable ($output_1), but you are doing a
"print" without an argument (which prints $_).

If you said "print $output_1" (or didn't stipulate a loop variable) it
would work.

olso...@yahoo.it

unread,
Jan 10, 2006, 1:38:48 PM1/10/06
to
Dear Dave,
Thanks a lot. This was my mistake. I modified my code and now it
works.
Regards,
O.O.

0 new messages