Errorlevel is not % errorlevel %

Source: Internet
Author: User
Errorlevel is not % errorlevel %

Http://blogs.msdn.com/oldnewthing/archive/2008/09/26/8965755.aspx

The Command Interpretercmd.exeHas a concept known asError level, Which is the exit code of the program most recently run. You can test the error level withIF ERRORLEVELCommand:

IF ERRORLEVEL 1 ECHO error level is 1 or more

<Sidebar>
TheIF ERRORLEVEL nTest succeeds if the error level is nOr more. This was presumably because there were programs that expressed different degrees of failure with higher and higher exit codes. For example,diffProgram has three exit codes: 0 means the files are the same; 1 means the files are different; 2 means that something terrible happened. there are also programs that use an exit code of zero to mean success and anything else to mean failure.
</Sidebar>

In addition to this internal state, you can, if you wish, create an environment variable with the nameERRORLEVEL, In the same way that you can create an environment variable calledFRED. But, asFRED, That variable won't have any effect on the error level.

rem this next command sets the error level to zeroCMD /C EXIT 0set ERRORLEVEL=1if ERRORLEVEL 1 echo Does this print?

The message is not printed becauseERRORLEVELEnvironment variable has no effect on the error level. It's just a variable whose name happens to coincide with a command processor concept.

set BANKBALANCE=$1,000,000.00

"Hey, when I tried to withdraw the money, I got an insufficient funds error. What am I doing wrong? "

Now, it does happen to be the case that if command extensions are enabled and you say%ERRORLEVEL%, Then the command processor first looks for an environment variable calledERRORLEVEL, AndIf it can't find one, Then it replaces%ERRORLEVEL%With the current value of the internal error level value. it's a fallback step, in the same way that your neighbor is a fallback delivery location if you aren't home. if you file a change-of-address form for yourself, that doesn't affect packages sent to your neighbor.

The same behavior can be seen%CD%: If you did not explicitly set an environment variable calledCD, Then%CD%Expands to the command processor's current directory. But you can't change directories by sayingset CD=C:\Windows.

I can think of a few reasons why this feature may have been added.

  • So you can include the error level in a log file:
    ECHO error level is %ERRORLEVEL%>logfile
  • So you can perform other types of tests against the error level, for example, to perform an authentication ity test:
    IF %ERRORLEVEL% EQU 1 echo Different!

But I'm digressing. My point for today is that the error level is not the same asERRORLEVELEnvironment variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.