我正在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%
)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我尝试使用exit/b,但看起来和我一样.任何的想法?
这出现飘飞,恕我直言,退出和退出/ 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 (
...
)
Run Code Online (Sandbox Code Playgroud)
但是如果你想使用&&或|| syntax(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
Run Code Online (Sandbox Code Playgroud)
使用"普通" exit /b然后调用它call myscript.cmd&&someotherapp.exe确实有效,但你不能假设执行批处理文件的每个程序都会创建进程cmd.exe /c call yourscript.cmd
| 归档时间: |
|
| 查看次数: |
4370 次 |
| 最近记录: |