Solved

perl & errorlevel

Posted on 1997-09-04
4
1,913 Views
Last Modified: 2008-03-04
is there a way to import back into perl  
dos errorlevel after executing
system(<some DOS command>);
from a perl program
0
Comment
Question by:weinerk
4 Comments
 

Expert Comment

by:mgjv
ID: 1206006
> perldoc -f system

=item system LIST

Does exactly the same thing as "exec LIST" except that a fork is done first, and the parent process waits for the child process to complete. Note that argument processing varies depending on the number of arguments. The return value is the exit status of the program as returned by the wait() call. To get the actual exit value divide by 256. See also L</exec>. This is I<NOT> what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in L<perlop/"`STRING`">.

I suspect that it works the same on all platforms, meaning that the DOS error level should be returned, multiplied by 256

0
 
LVL 1

Expert Comment

by:veazey
ID: 1206007
I think mgjv is on the right track.  In UNIX I would just try something like...

$ouput = system("some","command");

Then the output from some command is returned and stored in $output.  Try this on your PC.  Good luck.
0
 
LVL 2

Accepted Solution

by:
dmethvin earned 100 total points
ID: 1206008
The problem is that COMMAND.COM (the shell) doesn't pass back the exit status of the last command. Also, some builtin commands like DIR don't set (change) errorlevel at all. The only way I could figure out to get the status is to use a batch file as a helper that checks errorlevel and either sends it back as the output or writes it to a temp file for later inspection. Here's an example...send the children out of the room:

REM HELPER.BAT

REM The command to be tested. Find will return 1 if the string
REM isn't found, 2 if it is.  So to test, call HELPER using
REM HELPER.BAT as the arg and then some other file that
REM doesn't have "nauseum" in it.

find "nauseum" %1

REM Check errorlevel. This must be done in descending order
REM because errorlevel implies greater-than-or-equal. I only
REM care about three returns here so I didn't do all 255 codes.
REM If the commands you run don't have output it would
REM probably be easier to echo the exit status directly to
REM stdout and capure it in Perl with backticks.

if errorlevel 255 goto ERR255
if errorlevel 2   goto ERR2
if errorlevel 1   goto ERR1
rem no error
echo 0 >err.dat
goto DONE
:ERR255
echo 255 >err.dat
goto DONE
REM Ad nauseum here
:ERR2
echo 2 >err.dat
goto DONE
:ERR1
echo 1 >err.dat
:DONE

REM The errorlevel is now in ERR.DAT, the Perl program
REM can read that and do something.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1206009
If the problem is that the shell isn't passing back the exit status,
might you be able to bypass COMMAND.COM the same way you can bypass /bin/sh by calling
          $ouput = system('command', $arg1, $arg2);
instead of
          $ouput = system("command $arg1 $arg2");
?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now