2

I have a DOS batch file that calls a Perl Script file:

ssutexec.pl prog=MyProg I_CARD=MyCard O_F071=ME007 F70P=MYF70P TRIG=MYTRIG
set status=%errorlevel%
if NOT %status% == 0 (
  echo *******************************************************
  echo   MPSBM070 JOB stopped with a RETURN CODE of %status%
  echo ************** END OF JOB *****************************
  goto JobEnd
 )

Inside the ssutexec.pl perl script file, an error condition is executed and the script exits with exit($status):

if ($status != 0)
 {
  system("ssutdttm.pl SSUTEXEC \"finished processing $prog_nam (Abnormal End) on\"") ;
  print "******************************************************************" ;
  print "              JOB STOPPED - Due to Return code of ($status)" ;
  print "*****************END OF JOB***************************************" ;
  system("ssutdttm.pl SSUTEXEC \"finished processing $prog_nam (Abnormal End) on\"") ;
  system("ssuttmdr.pl -c -b $starttime -m \"$prog_nam\"") ;
  exit($status) ;
 }

I am not a perl script guy. Errorlevel isn't being triggered so I am not getting the error. How can I trap the value of the perl script exit code within my DOS batch file?

INDLUDING EDITS: The new batch file looks like this:

@echo off
setlocal enabledelayedexpansion 


if not defined run_env goto ScriptExit

echo ******************************************************************
echo *                      MYPROG                                  *
echo ******************************************************************

perl ssutdttm.pl MYPROG Start -
perl ssutexec.pl prog=MyProg I_CARD=MyCard O_F071=ME007 F70P=MYF70P TRIG=MYTRIG
set status=!errorlevel!
if NOT %status% == 0 (
  echo *******************************************************
  echo   MYPROG JOB stopped with a RETURN CODE of %status%
  echo ************** END OF JOB *****************************
  goto JobEnd
 )

:JobEnd
  if %status% == 0 (
    echo *******************************************************
    echo       NORMAL END OF JOB - RETURN CODE of %status%
    echo ******** NORMAL END OF JOB ****************************
   ) ELSE (
    echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    echo     ABNORMAL END OF JOB - RETURN CODE of %status%
    echo !!!!!! ABNORMAL END OF JOB !!!!!!!!!!!!!!!!!!!!!!!!!!!!
   )
  ssutdttm.pl MYPROG end - 
  exit /b %status%
::
:ScriptExit
echo run_env: %run_env% 
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132

2 Answers2

2

Working with batch files can really be a pain (DOS batch files make bash scripts look wonderful, and bash stinks!). The problem could be cropping up from the way batch files handle variable expansion. Here's something to try:

  1. Add the following line to the top of your batch script (I usually put it at the top, just under my @echo off line): setlocal enabledelayedexpansion
  2. Change all %ERRORLEVEL% references to !ERRORLEVEL! instead (exclamation marks instead of percent signs).

These changes will have the effect of causing variables to get expanded at execution time, rather than at parse-time. I've seen the %ERRORLEVEL% value get mangled when not using the delayed expansion option. Read this EnableDelayedExpansion information page for more.

Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
2

I think you should be calling perl.exe with the script as an argument:

perl ssutexec.pl prog=MyProg ...

In my tests, if I have just the script name, I get prompted for what I want to use to open the test.pl file. That means perl.exe is not executing my Perl code.

Your file associations could likely be there, but your script could still be failing because of the nuances of Windows.

I tested with my two very simple scripts:

test.bat

@ECHO OFF

perl test.pl

set status=%errorlevel%

if NOT %status% == 0 (
 echo status was set with value of %status%
)

and test.pl

use warnings;
use strict;

my $status = 44;

exit($status)

and the expected output:

C:\Dropbox\Programming\Perl\Learn\blah>test
status was set with value of 44
Community
  • 1
  • 1
Craig Treptow
  • 834
  • 7
  • 19
  • 2
    The [strict](http://perldoc.perl.org/strict.html) pragma enables a bunch of safety checks on your code. Using it in production environments is encouraged. Don't be surprised if your script initially chokes, though; it's not called "strict" for nothing. – Jonah Bishop Jan 24 '13 at 01:02
  • @Craig....I tried the above scenario and it worked with test.pl being called by perl and without. Andy ideas? – MikeTWebb Jan 24 '13 at 15:00