Windows批处理退出选项b带有或不带有错误级别 [英] Windows batch exit option b with or without errorlevel

查看:73
本文介绍了Windows批处理退出选项b带有或不带有错误级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在Windows上使用批处理的新手,并且对使用错误级别有疑问.

I'm newbie in using batch on Windows and have a question about the use of errorlevel.

我在Google上引用了 TechNet(Exit)和许多示例.

I referenced TechNet(Exit) and many examples on google.

其中大多数使用/b并具有%errorlevel%这样的效果

Most of them used /b with %errorlevel% like this

if errorlevel 1 exit /b %errorlevel%

我想知道两者之间的区别

I wonder the difference between

if errorlevel 1 exit /b

if errorlevel 1 exit /b %errorlevel%

我认为没有区别,因为%errorlevel%不变.我错了吗?

I think there are no difference because %errorlevel% is not changed. Am I wrong?

推荐答案

TL; DR

大多数情况下应该没有区别,但是从技术上讲,如果要退出而没有任何退出,严格来说 exit/b%errorlevel% exit/b 差更改错误级别.

EXIT/B 不会更改错误级别,因此,作为独立命令,它完全等同于 EXIT/B%errorlevel% 只要%errorlevel%可以解决当前的错误级别.

EXIT /B without the optional errorlevel parameter does not change the error level, so as a standalone command it is exactly equivalent to EXIT /B %errorlevel% provided that %errorlevel% resolves to the current error level.

但是在某些情况下它可能不会这样做:

But there are cases where it might not do so:

  • 如果定义了名为 ERRORLEVEL 的环境变量,则%errorlevel%始终解析为其值(可以是任意值),而不是当前错误级别./li>
  • 如果禁用了命令扩展名,则%errorlevel%将永远不会解析为当前的错误级别(如果定义,它仍将读取具有该名称的环境变量的值).您可以通过使用 CMD/E:OFF 启动命令提示符并尝试 ECHO%errorlevel%来验证这一点.
  • %errorlevel%产生的当前错误级别值将在解析命令时固定,而不是在执行到达该表达式时固定.这可能会导致为更复杂的命令生成错误的值.示例:

  • If an environment variable named ERRORLEVEL is defined then %errorlevel% always resolves to its value (which can be arbitrary), and not to the current error level.
  • If command extensions are disabled then %errorlevel% will never resolve to the current error level (it will still read the value of the environment variable with that name, if defined). You can verify this by starting a command prompt with CMD /E:OFF and trying ECHO %errorlevel%.
  • The current error level value as produced by %errorlevel% will be fixed at the time the command is parsed, not at the time execution reaches that expression. This can result in the wrong value being produced for more complex commands. Example:

copy j:\not_existing q:\not_existing & echo %errorlevel%

这不会产生与

copy j:\not_existing q:\not_existing
echo %errorlevel%

因为在第一种情况下,%errorlevel%不会产生由失败的 copy 引起的更新的错误级别.

because in the first case %errorlevel% will not produce the updated error level caused by the failed copy.

这篇关于Windows批处理退出选项b带有或不带有错误级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆