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!

Process Windows commands and get return code 4

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
Hi all,
Trying to run Windows command and process return code, then use return codes to process code

In Win Command line:

c:\command
results here
c:\echo %errorlevel%
30 (for example)

In Perl, I try to run the following code

$output = `command`;
$errorlevel = `echo %errorlevel%`;
print $output; #just to view results
print $errorlevel; #just to review results

$errorlevel always returns 0 instead of 30, because I think it is started in a different shell to the `command` system call.

How do I tie the two commands together so that I get the same result as running it from the command line?

Thanks
Steve
 
Code:
my @output;
my $errorlevel=system(qq(command > myoutput));
if ($errorlevel) {
    print qq("command" returned status "$errorlevel");
} else {
    open(MYOUTPUT, "myoutput") || 
        die qq(Can't open "myoutput" for read\n);
    @output = <MYOUTPUT>;
    print qq($_\n) for (@output);
}
 
mikevh,

sorry, this didn't produce the correct result

result of running the script was

c:\command returned status 5888

Should be 30

Any other ideas??

 
Yes, use the special variable $?.

my $returned_text = `xcopy blah do`;
print $? >> 8;

From Teach Yourself Perl in 21 Days said:
The $? variable returns the error status generated by calls to the system function or by calls to functions enclosed in back quotes, as in the following:

$username = 'hostname';


The error status stored in $? consists of two parts:

* The exit value (return code) of the process called by system or specified in back quotes
* A status field that indicates how the process was terminated, if it terminated abnormally

The value stored in $? is a 16-bit integer. The upper eight bits are the exit value, and the lower eight bits are the status field. To retrieve the exit value, use the >> operator to shift the eight bits to the right:

$retcode = $? >> 8;


Alternatively, you can divide the error return variable by 256 and get the same result.

my $returned_text = `xcopy blah do`;
print $? / 256;


 
Thanks heaps Raklet, that did the job!
 
hi cheungs
i would be of great help to understand it better if you can post the simple working script

 
Yes, thanks, raklet. I'll remember that, I hope.
 
envetri,

I basically replicated what Raklet suggested

$output = `windows command here`; # produces a return code
$retval =$?/256; # $? is the return code from Win command
print "Return code is: ",$retval,"\n";
if ($retval eq 23)
{
do this;
}
elsif ($retval eq 24)
{
do that;
}
else
{
etc etc;
}

Works beautifully. I just had no idea how to get the return value into a variable using Perl...until now.

HTH
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top