如何同时退出cmd文件和shell?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
时不时就会出现这种情况然后,IMHO exit 和 exit /b 被破坏,因为它们只设置批处理文件使用的错误级别,但它们不设置 cmd.exe 进程的退出代码。
如果批处理脚本正在执行错误级别检查,则调用就足够了:
但是如果您想使用 &&或||语法 (
myscript.cmd&&someotherapp.exe
) 或您的脚本是从程序而不是另一个批处理文件启动,您实际上想要设置进程退出代码(使用 GetExitCodeProcess 在父进程中)使用“正常”< 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:
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)Using a "normal"
exit /b
and then calling it withcall myscript.cmd&&someotherapp.exe
does work, but you can't assume that every program that executes a batch file will create the process ascmd.exe /c call yourscript.cmd
这一切都与实际运行脚本的 shell 有关。当脚本执行时,它在子 shell 中运行,因此调用 exit 只是退出该子 shell。但是,我认为如果您使用 call 语句执行脚本,它将在该 shell 的上下文中执行,而不是执行子 shell。
因此,要执行脚本
,而不仅仅是
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
and not just
您可以(ab)使用
GOTO
当标签不存在且条件执行为负时出现错误。在这种情况下,cmd.exe
从批处理脚本模式切换到命令提示符模式并可以退出:You can (ab)use
GOTO's
bug when it is with non existent label and negative conditional execution.In this case thecmd.exe
from batch script mode to command prompt mode and can be exited: