如何退出同时一个cmd文件和壳? [英] How to exit a cmd file and the shell at the same time?

查看:127
本文介绍了如何退出同时一个cmd文件和壳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行外壳内的脚本(.CMD)(C:\\ WINDOWS \\ SYSTEM32 \\ CMD.EXE)。我要的是,当一个指令C .cmd文件结束其执行,然后CMD.EXE也结束了执行返回错误code到调用它的人。返回一个错误$ C $

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?

推荐答案

这来了<一个href=\"http://stackoverflow.com/questions/4632891/exiting-batch-with-exit-b-x-where-x1-acts-as-if-command-completed-successfull\">every现在,然后,恕我直言,退出和退出/ B被打破,因为它们只通过设置批处理文件中使用的错误级别,但他们不设置cmd.exe进程的退出code。

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 )或脚本从程序开始,而不是另一个批处理文件,你居然要设置的进程退出code(检索与 GetExit codeProcess 中的父进程)

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

使用正常退出/ B ,然后用调用它调用myscript.cmd&放大器;&安培; someotherapp.exe 的工作,但你不能假定每个执行批处理文件程序将创建进程的cmd.exe / c调用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文件和壳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆