murillo@sigi.Colorado.EDU (Rodrigo Murillo) (03/15/88)
I posted an article asking how to set DOS errorlevel from a C program.
Someone suggested exit(errorlevel). But wait...
The following code shows that exit(errorlevel) works. The problem is
that if errorlevel is set with an exit(5) for example, all if tests for
values <= to 5 are tested as true. This is some sample output showing the
results of passing 5 parameters to the batch file, which calls ERRLEVEL
which does an exit(5):
F:\>errtest 1 2 3 4 5
F:\>echo off
exit(5);
FIVE!
FOUR!
THREE!
TWO!
ONE!
ZERO!
F:\>
If anybody can tell why this is the way it is I would love to hear it.
Why would (errorlevel 0) test true if the exit is exit(5)??!!
==>errlevel.c
/* exit(n) with the number of paramters passed to it */
main(argc, argv)
int argc;
char *argv[];
{
printf("\nexit(%d);\n",argc-1);
exit(argc-1);
}
/* end of program */
==>errtest.bat
echo off
errlevel %1 %2 %3 %4 %5 %6 %7 %8
if errorlevel 8 echo EIGHT!
if errorlevel 7 echo SEVEN!
if errorlevel 6 echo SIX!
if errorlevel 5 echo FIVE!
if errorlevel 4 echo FOUR!
if errorlevel 3 echo THREE!
if errorlevel 2 echo TWO!
if errorlevel 1 echo ONE!
if errorlevel 0 echo ZERO!
--
_______________________________________________________________________________
Rodrigo Murillo, University of Colorado - Boulder (303) 761-0410
murillo@boulder.colorado.edu | ..{hao|nbires}!boulder!murillo
( Machines have less problems. I'd like to be a machine. -- Andy Worhol )
bentrup@uiucdcsp.cs.uiuc.edu (03/16/88)
>/* Mar 14, 1988 by murillo@sigi.Colorado.EDU in uiucdcsp:comp.sys.ibm.pc */ >The following code shows that exit(errorlevel) works. The problem is >that if errorlevel is set with an exit(5) for example, all if tests for >values <= to 5 are tested as true. > exit(5); > FIVE! > FOUR! > ... > ZERO! >Why would (errorlevel 0) test true if the exit is exit(5)??!! >if errorlevel 8 echo EIGHT! > ... >if errorlevel 1 echo ONE! >if errorlevel 0 echo ZERO! From the manual "ERRORLEVEL number is true if the previous program had an exit code of number or higher" ^^^^^^ I think you want to do something akin to if errorlevel 8 goto process_8 if errorlevel 7 goto process_7 ... :process_8 <whatever error 8 does> goto exit :process_7 <etc for 7> goto exit ... :exit john John Bentrup University of Illinois at Urbana-Champaign Department of Computer Science arpa bentrup@a.cs.uiuc.edu bentrup@uiuc.ARPA csnet bentrup%uiuc@csnet-relay usenet {pur-ee,convex,ihnp4}!uiucdcs!bentrup
james@bigtex.uucp (James Van Artsdalen) (03/16/88)
In article <4835@sigi.Colorado.EDU>, murillo@sigi.Colorado.EDU (Rodrigo Murillo) writes: > If anybody can tell why this is the way it is I would love to hear it. > Why would (errorlevel 0) test true if the exit is exit(5)??!! The larger the organization, the finer the line between a "feature" and a "design error". For this, send Bill Gates a thank you note. From the Compaq MS-DOS/Version 3 Reference Guide: "If the preceding program returns an exit code that is equal to or greater than the specified NUMBER, the condition is true. If you use the NOT parameter, the preceding error code must be less than NUMBER". So "errorlevel 0" is always true, and "not errorlevel 0" is always false. Enjoy. -- James R. Van Artsdalen ...!uunet!utastro!bigtex!james "Live Free or Die" Home: 512-346-2444 Work: 328-0282; 110 Wild Basin Rd. Ste #230, Austin TX 78746
bobmon@iucs.cs.indiana.edu (RAMontante [condition that I not be identified]) (03/16/88)
Rodrigo Murillo asks why the batch-file test if errorlevel N ... succeeds for any errorlevel value that is greater than or equal to N. (Note that this is the documented behavior.) This is something of a philosophical question. I think it most likely that somebody thought they were saving 5 or 6 bytes of code this way. A more forgiving guess might be that someone had a particular style of use or prior example in mind, although my MSDOS manual notes that "Most MS-DOS programs currently return an exit code of 0 at all times." Or perhaps it's best explained in the same way that one explains the rest of MSDOS, and indeed the machines that employ all this mess -- It is the way it is, in order to build the programmer's character.
emartins@uxh.cso.uiuc.edu (03/16/88)
The problem is that DOS tests ERRORLEVEL for a value GREATER THAN or EQUAL to whatever number you've specified on your batch file... So, you have to begin testing for the highest errorlevel. Try somethink like IF ERRORLEVEL 5 GOTO FOO1 IF ERRORLEVEL 4 GOTO FOO2 IF ERRORLEVEL 3 GOTO FOO3 GOTO END :FOO1 ECHO ERRORLEVEL IS FIVE! GOTO END :FOO2 ECHO ERRORLEVEL IS FOUR! GOTO END :FOO3 ECHO ERRORLEVEL IS THREE! :END This should work... Eduardo Martins (emartins@uxh.cso.uiuc.edu)
wew@naucse.UUCP (Bill Wilson) (03/16/88)
If you read your DOS manual you would know that the IF ERRORLEVEL statement tests for >= the errorlevel given. You have to reverse engineer your logic. Dumb of IBM and Microsoft but that's the way it goes. Bill Wilson.
hamilton@uxc.cso.uiuc.edu (03/18/88)
/* Written 9:57 am Mar 16, 1988 by wew@naucse.UUCP in uxc.cso.uiuc.edu:comp.sys.ibm.pc */ If you read your DOS manual you would know that the IF ERRORLEVEL statement tests for >= the errorlevel given. You have to reverse engineer your logic. Dumb of IBM and Microsoft but that's the way it goes. Bill Wilson. /* End of text from uxc.cso.uiuc.edu:comp.sys.ibm.pc */ what's so dumb about it? if you want to check individual values, you can; just check for them in decreasing order. imagine tho that it had been implemented to test for exact match, and you wanted to check for >=! wayne hamilton U of Il and US Army Corps of Engineers CERL UUCP: {ihnp4,seismo,pur-ee,convex}!uiucuxc!hamilton ARPA: hamilton@uxc.cso.uiuc.edu USMail: Box 476, Urbana, IL 61801 CSNET: hamilton%uxc@uiuc.csnet Phone: (217)333-8703 CIS: [73047,544] PLink: w hamilton