如何退出cmd文件和shell同时? [英] How to exit a cmd file and the shell at the same time?
问题描述
我正在shell(c:\windows \system32 \cmd.exe)中执行脚本(.cmd)。我想要的是,当一个命令返回一个错误代码.cmd文件结束其执行,然后cmd.exe也结束其执行返回的错误代码到调用它。
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.
我使用这样的:
C:\...\gacutil.exe /i C:\...\x.dll
if not errorlevel 0 (
echo Error registering C:\...\x.dll
exit %errorlevel%
)
但这不起作用。我尝试与退出/ B,但看起来一样的我。
But this does not work. I tried with exit /b but looks the same to me. Any idea?
推荐答案
这会出现现在,然后,IMHO退出和退出/ b断开,因为它们只设置使用的错误级别批处理文件,但它们不设置cmd.exe进程的退出代码。
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 (
...
)
但是如果你想使用&或||语法( myscript.cmd&& someotherapp.exe
)或者您的脚本从程序而不是另一个批处理文件启动,实际上您想要设置进程退出代码在父进程中 GetExitCodeProcess )
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
使用正常 exit / b
然后用调用myscript.cmd&& someotherapp.exe
调用它,但你不能假设每个程序执行批处理文件将创建过程 cmd.exe / c call yourscript.cmd
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
这篇关于如何退出cmd文件和shell同时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!