windows 如何在不关闭命令提示符窗口的情况下返回错误代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14905876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return an error code without closing the Command Prompt window?
提问by Martin
I am writing a batch file which validates a couple of files. When one of the file isn't valid, I want the batch script to stop and return an error code >0. The code below seem to do the job, but calling "EXIT 2" closes the Command Prompt window in which the script was running.
我正在编写一个批处理文件来验证几个文件。当其中一个文件无效时,我希望批处理脚本停止并返回错误代码 >0。下面的代码似乎可以完成这项工作,但调用“EXIT 2”会关闭运行脚本的命令提示符窗口。
:Validate
SETLOCAL
Validator %1
IF %ERRORLEVEL% GEQ 1 EXIT 2
ENDLOCAL
Any idea on how to return an error code without closing the Command Prompt?
关于如何在不关闭命令提示符的情况下返回错误代码的任何想法?
回答by Hans Passant
To get help for command prompt commands use their /? option. Exit /?
shows:
要获得命令提示符命令的帮助,请使用他们的 /? 选项。 Exit /?
显示:
Quits the CMD.EXE program (command interpreter) or the current batch script.
EXIT [/B] [exitCode]
/B specifies to exit the current batch script instead of CMD.EXE. If executed from outside a batch script, it will quit CMD.EXE
exitCode specifies a numeric number. if /B is specified, sets ERRORLEVEL that number. If quitting CMD.EXE, sets the process exit code with that number.
退出 CMD.EXE 程序(命令解释器)或当前的批处理脚本。
退出 [/B] [退出代码]
/B 指定退出当前批处理脚本而不是 CMD.EXE。如果从批处理脚本外部执行,它将退出 CMD.EXE
exitCode 指定一个数字。如果指定了 /B,则设置 ERRORLEVEL 该数字。如果退出 CMD.EXE,请使用该数字设置进程退出代码。
So you want
所以你要
IF %ERRORLEVEL% GEQ 1 EXIT /B 2
回答by Pascal Belloncle
You can use the pause
command before calling exit.
您可以pause
在调用 exit 之前使用该命令。
If you don't like the message:
如果您不喜欢该消息:
pause > nul
回答by Hemakumar
Got the same issue. If you are writing a batch (windows shell script). 'cmd' should do it for you. this wont exit the batch and remains at the command prompt.
Solved my problem.
for ex:
cd "\view\Flex Builder 3\gcc-mvn"
set path="c:\view\jdk1.7.0_02\bin";"c:\view\apache-maven-3.0.5\bin";%path%
mvn sonar:sonar
cmd
should remain at the prompt after the execution.
得到了同样的问题。如果您正在编写批处理(Windows shell 脚本)。' cmd' 应该为你做。这不会退出批处理并保留在命令提示符下。解决了我的问题。例如:
cd "\view\Flex Builder 3\gcc-mvn"
set path="c:\view\jdk1.7.0_02\bin";"c:\view\apache-maven-3.0.5\bin";%path%
mvn sonar:sonar
cmd
执行后应保持在提示符处。