奇怪的 CMD 批处理脚本报告 %errorlevel%

发布于 2024-10-16 15:35:55 字数 830 浏览 4 评论 0原文

在下面的脚本中,当出现 sql 错误时,%errorlevel% 为 0 - 这是为什么?

IF %RELEASE% GTR 2 (
    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    echo error: %errorlevel%
    if %errorlevel% gtr 0 goto dberror
)   

如果我删除 if 块(如下),则 %errorlevel% 不为零!为什么if语句会影响错误级别?

    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    echo error: %errorlevel%
    if %errorlevel% gtr 0 goto dberror

更新:相信这是我测试错误的方式。我认为而不是:

if %errorlevel% gtr 0 goto dberror

..应该使用:

if errorlevel 1 goto dberror

有用的链接这里

In the script below, when there is a sql error, %errorlevel% is 0 - why is this?

IF %RELEASE% GTR 2 (
    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    echo error: %errorlevel%
    if %errorlevel% gtr 0 goto dberror
)   

If I remove the if block (below) then %errorlevel% is NON zero! Why does the if statement affect the error level?

    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    echo error: %errorlevel%
    if %errorlevel% gtr 0 goto dberror

Update: Believe it was the way I was testing for an error. I think instead of:

if %errorlevel% gtr 0 goto dberror

.. should be using:

if errorlevel 1 goto dberror

Useful link here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-10-23 15:35:55

叹。这一切都与可怕的 dos 解析以及 cmd 用它的值替换 %errorlevel% 的点有关。当您用 (...) 括起命令时,cmd 首先读取这些命令,就像它们全部写在一行上一样,从而扩展变量就这样。因此,在第一个代码块中,%errorlevel% 在解析该块时被其值替换。。就好像您写道:(

IF 3 GTR 2 (
   (
   echo WHENEVER SQLERROR EXIT FAILURE
   echo @release.sql
   echo exit
   ) | sqlplus x/x@orcl
   echo error: 0
   if 0 gtr 0 goto dberror
)

假设 $RELEASE 为 3)。您的 if errorlevel 修复起作用的原因是 cmd 不会对该重新制定进行任何(太)早期的变量扩展。您可以使用 call 来避免这个问题:(

IF %RELEASE% GTR 2 (
    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    call echo error: %%errorlevel%%
    call if %%errorlevel%% gtr 0 goto dberror
)

我认为这比 startlocal ENABLEDELAYEDEXPANSION!errorlevel! - 当然是 YMMV 更清晰)。

Sigh. It's all to do with the horrible dos parsing, and the point at which cmd replaces %errorlevel% with its value. When you enclose commands with (...), cmd first reads in those commands as though they were all written on one line, expanding variables as it goes. Thus in your first code block, %errorlevel% is replaced by its value when the block is parsed. It's as if you wrote:

IF 3 GTR 2 (
   (
   echo WHENEVER SQLERROR EXIT FAILURE
   echo @release.sql
   echo exit
   ) | sqlplus x/x@orcl
   echo error: 0
   if 0 gtr 0 goto dberror
)

(assuming $RELEASE was 3). The reason your if errorlevel fix works is that cmd does not do any (too) early variable expansion on that re-formulation. You can use call to avoid this problem:

IF %RELEASE% GTR 2 (
    (
    echo WHENEVER SQLERROR EXIT FAILURE
    echo @release.sql
    echo exit
    ) | sqlplus x/x@orcl
    call echo error: %%errorlevel%%
    call if %%errorlevel%% gtr 0 goto dberror
)

(which I think is clearer than startlocal ENABLEDELAYEDEXPANSION and !errorlevel!—YMMV of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文