如何同时退出cmd文件和shell?

发布于 2024-10-15 07:08:03 字数 340 浏览 2 评论 0原文

我正在 shell (c:\windows\system32\cmd.exe) 内执行脚本 (.cmd)。我想要的是,当命令返回错误代码时,.cmd 文件结束其执行,然后 cmd.exe 也结束其执行,将错误代码返回给调用它的文件。

我正在使用这样的东西:

C:\...\gacutil.exe /i C:\...\x.dll
if not errorlevel 0 (
    echo Error registering C:\...\x.dll
    exit %errorlevel%
)

但这不起作用。我尝试使用 exit /b 但对我来说看起来是一样的。有什么想法吗?

I'm executing an script (.cmd) inside a shell (c:\windows\system32\cmd.exe). What I want is that when a command returns an errorcode the .cmd file ends its execution and then cmd.exe also ends its executing returning the errorcode to the one that called it.

I'm using something like this:

C:\...\gacutil.exe /i C:\...\x.dll
if not errorlevel 0 (
    echo Error registering C:\...\x.dll
    exit %errorlevel%
)

But this does not work. I tried with exit /b but looks the same to me. Any idea?

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

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

发布评论

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

评论(3

森林散布 2024-10-22 07:08:03

时不时就会出现这种情况然后,IMHO exit 和 exit /b 被破坏,因为它们只设置批处理文件使用的错误级别,但它们不设置 cmd.exe 进程的退出代码。

如果批处理脚本正在执行错误级别检查,则调用就足够了:

REM DoSomeAction.cmd
@echo off
call someprogram.exe
if errorlevel 1 exit /b 

REM MainScript.cmd
@echo off
...
call DoSomeAction.cmd
if errorlevel 1 (
  ...
)

但是如果您想使用 &&或||语法 (myscript.cmd&&someotherapp.exe) 或您的脚本是从程序而不是另一个批处理文件启动,您实际上想要设置进程退出代码(使用 GetExitCodeProcess 在父进程中)

@echo off
call thiswillfail.exe 2>nul
if errorlevel 1 goto diewitherror
...
REM This code HAS to be at the end of the batch file
REM The next command just makes sure errorlevel is 0
verify>nul
:diewitherror
@%COMSPEC% /C exit %errorlevel% >nul

使用“正常”< code>exit /b 然后用 call myscript.cmd&&someotherapp.exe 调用它确实有效,但你不能假设每个执行批处理文件的程序都会创建进程为 cmd.exe /c call yourscript.cmd

This comes up every now and then, IMHO exit and exit /b are broken since they only set the errorlevel used by batch files, but they do not set the exit code of the cmd.exe process.

If a batch script is doing the errorlevel checking, call is enough:

REM DoSomeAction.cmd
@echo off
call someprogram.exe
if errorlevel 1 exit /b 

REM MainScript.cmd
@echo off
...
call DoSomeAction.cmd
if errorlevel 1 (
  ...
)

But if you want to use && or || syntax (myscript.cmd&&someotherapp.exe) or your script is started from a program and not another batch file, you actually want to set the process exit code (Retrieved with GetExitCodeProcess in the parent process)

@echo off
call thiswillfail.exe 2>nul
if errorlevel 1 goto diewitherror
...
REM This code HAS to be at the end of the batch file
REM The next command just makes sure errorlevel is 0
verify>nul
:diewitherror
@%COMSPEC% /C exit %errorlevel% >nul

Using a "normal" exit /b and then calling it with call myscript.cmd&&someotherapp.exe does work, but you can't assume that every program that executes a batch file will create the process as cmd.exe /c call yourscript.cmd

奢华的一滴泪 2024-10-22 07:08:03

这一切都与实际运行脚本的 shell 有关。当脚本执行时,它在子 shell 中运行,因此调用 exit 只是退出该子 shell。但是,我认为如果您使用 call 语句执行脚本,它将在该 shell 的上下文中执行,而不是执行子 shell。

因此,要执行脚本

call <script.cmd>

,而不仅仅是

<script.cmd>

It's all about the shell that's actually running the script. When a script executes it is run in a subshell, so calling exit just exits that subshell. However, I think that if you execute the script by using the call statement it will execute within the context of that shell and not execute a subshell.

So, to execute the script use

call <script.cmd>

and not just

<script.cmd>
陪你搞怪i 2024-10-22 07:08:03

您可以(ab)使用GOTO 当标签不存在且条件执行为负时出现错误。在这种情况下,cmd.exe 从批处理脚本模式切换到命令提示符模式并可以退出:

C:\...\gacutil.exe /i C:\...\x.dll
if not errorlevel 0 (
    echo Error registering C:\...\x.dll
    goto :no_such_label >nul 2>&1 || exit %errorlevel%
)

You can (ab)use GOTO's bug when it is with non existent label and negative conditional execution.In this case the cmd.exe from batch script mode to command prompt mode and can be exited:

C:\...\gacutil.exe /i C:\...\x.dll
if not errorlevel 0 (
    echo Error registering C:\...\x.dll
    goto :no_such_label >nul 2>&1 || exit %errorlevel%
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文