1

i am trying to run a batch file from a perl script but i am not able to handle the return of a batch file. below is my code

perl script:

// call a batch file 
        my $output =system("D:\\WORKSPACE\\CC_Triggers\\batch_file.bat");
// based on output check the status of the batch file
        if($output==0){
        print "success in trigger**********";
        }
        else
        {
        print "FAilure**********";
        }

batch file:

set "java_output="
setlocal enableDelayedExpansion
//this will call a java program and get return value.
for /f "delims=" %%J in ('java -cp "PMDfileRead.jar;jxl.jar" PMDfileRead') do (
      set "java_output=!java_output! %%J" 
)
endlocal & set java_output=%java_output%

// check the ouput of java and act accordingly
IF %java_output% == 1 (
echo failed
exit(1);
)else (
echo succeeded
)

basically i am trying to validate an xls file and return flag from java code which is being called by a batch file and from ther i need to return back to perl script which i am not able to get. in case of success and failure, i am getting as "0" as output due to which output is success.. request to please correct my code or suggest me the best way to handle this.

TLP
  • 66,756
  • 10
  • 92
  • 149

2 Answers2

0

Replace your system() call with backticks instead:

A system() wrapped call returns the exit status of the command, whereas using ``'s returns the actual STDOUT (i.e. your echo statements)

For more info: What's the difference between Perl's backticks, system, and exec?

i.e.: my $output = `D:\WORKSPACE\CC_Triggers\batch_file.bat`;

Community
  • 1
  • 1
joeycato
  • 118
  • 2
  • 11
  • It appears the Op actually wants to check against the exit code. Of course, they could, as an alternative, use backticks and check against the echoed stuff. – DeVadder Feb 18 '14 at 07:58
  • hi thanks for the reply ! now after replacing with your code m not getting any output from the batch file :(. please help – user3274878 Feb 18 '14 at 09:03
0

edit: Sorry i somehow read past the comment by mpapec stating the same thing.

The Perl part is fine. If you always get a 0, it has to be the batch script acting up. Or the Java. If you want to inspect the return value in Perl for anything except whether it is zero or not, then you would have to shift it right 8 bits.

my $output = (system("D:\\WORKSPACE\\CC_Triggers\\batch_file.bat")) >> 8;

But as you only check for zero or not, that should not be an issue here.

edit: When using Windows, it is important to never define an environment variable ERRORLEVEL as that will mix up with the actual exit codes returned by applications. This is explained in more detail here: How do I get the application exit code from a Windows command line?

Community
  • 1
  • 1
DeVadder
  • 1,404
  • 10
  • 18
  • but in case of failures m getting response as 0. That is not desired one. – user3274878 Feb 18 '14 at 09:42
  • @user3274878: Well, do you also see the `failed` on console from the batch file? And if you execute your batch file from the command line and follow it with `echo Exit Code is %errorlevel%` does that give you 1 or 0 in case of failure? – DeVadder Feb 18 '14 at 10:48
  • yes it is printign "failed" on console. and for echo %errorlevel% it is 1 always since it is created as environment vaiable in windows – user3274878 Feb 19 '14 at 06:42
  • @user3274878 I can't help you then. The perl part you posted works and does roughly what you think it does. If the script called in `system()` returns a 1, then `$output >> 8` is 1. And that means `$output` is not zero. If it does not work for you, then it has something to do with the batch part or with your windows magically altering exit codes. Maybe delete `errorlevel` for a test. – DeVadder Feb 19 '14 at 07:08
  • HI , finally i got it . i need to delete one ENV variable which i created %ERRORLEVEL% which was set to 1 and after removing that i am getting the desired output.... – user3274878 Feb 20 '14 at 05:34
  • thanks alot for all the experts in stackoverflow ... :) – user3274878 Feb 20 '14 at 05:35