Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return value from a process

Status
Not open for further replies.

matvashy

Programmer
Aug 10, 2006
3
US
Hi,
Can someone suggest a way to return a value from a script that gets called from the main script using "system" comand back to the main script. What I know is that system command returns status of execution, but not the other content.

Im a newbee in perl :)

Thanks
 
Code:
@results=`perl script2.pl`;
foreach(@results) { print; }
also qx
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
open(READ,'ls /usr/bin/ |');
print "Start of output from ls command\n";
while(<READ>){
    print $_, "\n";
}
print "end of output from ls command\n";

dmazzini
GSM System and Telecomm Consultant

 
Thanks for your replies, guys, but i guess i need to explain myself a little bit clearer.

I have a main script (main.pl) which calls and executes other
perl scripts within itself using "system" utility (forks off new process in its). System returns only exit status of each execution which in case if a scipt didnt crash is 0. But i need to return a value to main from each of the scripts, not just the status. So, this is what i'm stummbled upon.

One more time, thanks to all who responded. regards,
 
Code:
#!/usr/bin/perl
# add a user who already exists
`/usr/sbin/useradd root 2>/dev/null`;
# if error code, return it
if ($?)
{
print "Error code ", $? >> 8;
}


Here's the output:


Error code 9

[\code]


So I guess you can do


@data = `/usr/sbin/useradd root 2>/dev/null`;

@data will have the output
$?  will have Error code

You can get more information reading perl special variables.

In case you're wondering about the bitwise operation in the program above -
the value stored in the $? variable is a 16-bit integer, of which the first 8 bits represent the error code returned by the invoked command.

You can also use the $? & 127 operation to obtain information on the termination signal of the command, and $? & 128 operation to get a Boolean value indicating whether or not the program dumped core.

Cheers


dmazzini
GSM System and Telecomm Consultant

 
I have a main script (main.pl) which calls and executes other
perl scripts within itself using "system" utility (forks off new process in its). System returns only exit status of each execution which in case if a scipt didnt crash is 0. But i need to return a value to main from each of the scripts, not just the status. So, this is what i'm stummbled upon.

You can't do it. system() does not return a value other than the exit status. You have to use backtiks or the qx// operator if you want to get the return value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top