output of system() call

  • Thread starter Madhu Ramachandran
  • Start date
M

Madhu Ramachandran

question.

i am perl newbie. i read that system() call can be used to invoke other
program (shell scripts etc). The return code of the program is returned by
system() call and i have to >> by 8 bits.

is there any variable which will contain ouput printed by the program in
system() call?

eg:
system("mytest.sh");
if mytest.sh has echo "bla" in it. how can i capture the 'bla' value, using
the system() call?

currently iam doing this.
system ("mytest.sh > /tmp/outfile");
# I open this /tmp/outfile and use $line = <outFp>; and get the value.

wondering if there is an easier way to get output.

-Madhu
 
P

Paul Lalli

Madhu said:
i am perl newbie. i read that system() call can be used to invoke other
program (shell scripts etc). The return code of the program is returned by
system() call and i have to >> by 8 bits.

is there any variable which will contain ouput printed by the program in
system() call?

Read the documentation for the function you're using. It contains the
answer to your question.

perldoc -f system
(3rd paragraph)

Paul Lalli
 
T

Tad McClellan

question.


You put that in there to distinguish from the many posts here
that do not ask a question?

i am perl newbie.


You really should see the Posting Guidelines you know.



Where did you read it?

In the documentation for the software that you are using?

that system() call can be used to invoke other
program
is there any variable which will contain ouput printed by the program in
system() call?


No, but there is a way to capture the output printed by the program
into a variable using something other than the system() function.

how can i capture the 'bla' value, using
the system() call?


You can't.

You'll have to use something other than the system() function.

wondering if there is an easier way to get output.


Yes there is, and it is included in the documentation for the
system() function that you already know about.

Why didn't you read the documentation for the function you are using?


By now you have used up all of your coupons...

So long!
 
J

Jürgen Exner

Madhu said:
i am perl newbie. i read that system() call can be used to invoke
other program (shell scripts etc). The return code of the program is
returned by system() call and i have to >> by 8 bits.

is there any variable which will contain ouput printed by the program
in system() call?

Yep. It works exactly as described in paragraph 3, sentence 4 of the
documentation of the very function that you are using.

jue
 
Z

zentara

question.

i am perl newbie. i read that system() call can be used to invoke other
program (shell scripts etc). The return code of the program is returned by
system() call and i have to >> by 8 bits.

is there any variable which will contain ouput printed by the program in
system() call?

eg:
system("mytest.sh");
if mytest.sh has echo "bla" in it. how can i capture the 'bla' value, using
the system() call?

currently iam doing this.
system ("mytest.sh > /tmp/outfile");
# I open this /tmp/outfile and use $line = <outFp>; and get the value.

wondering if there is an easier way to get output.

Here is a super simple example using IPC::Open3. Of course,
you don't need the bash, you can run mytest.sh directly.

There are many more techniques around to enhance your use
of IPC::Open3, google for them.
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
$|=1;

#my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/bash');
my $pid=open3(\*IN,\*OUT,0,'/bin/bash');
# set \*ERR to 0 to send STDERR to STDOUT

my $cmd = 'date';
#send cmd to bash
print IN "$cmd\n"; #need the \n with bash

#getresult
my $result = <OUT>;
print $result;

__END__
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top