简体   繁体   English

如何使用批处理文件获取Windows返回码

[英]How to get windows returncode withi a batch file

I have some perl scripts which are invoked by a windows batch. 我有一些Windows批处理调用的perl脚本。 The perl scripts provide returncodes which i need to output, but i alwas get 0. Even with a absolute reduced perl script: Perl脚本提供了我需要输出的返回码,但是我总是得到0。即使绝对减少了Perl脚本,也可以:

use Modern::Perl '2015';

exit 1;

I get 0 als ERRORLEVEL. 我得到0个同级错误。 Here's the part of my windows batch file, where i invoke the perl script: 这是我的Windows批处理文件的一部分,在这里我调用perl脚本:

cmd /c "C:\Strawberry32\portableshell.bat C:\Users\abc\error.pl"
echo ERRORLEVEL Error Script: %ERRORLEVEL%

The output is "ERRORLEVEL Error Script: 0" 输出为“ ERRORLEVEL错误脚本:0”

I already tried "start /wait ..." insted of cmd, but here i see a second console window, which i have to close manualy. 我已经尝试过由cmd插入的“ start / wait ...”,但是在这里我看到了第二个控制台窗口,我必须手动关闭它。 That's not what i want. 那不是我想要的。

If i enter "C:\\Strawberry32\\portableshell.bat C:\\Users\\abc\\error.pl" manually in a konsole window, i see/get the expected Errorlevel 1, what's wrong here? 如果我在konsole窗口中手动输入“ C:\\ Strawberry32 \\ portableshell.bat C:\\ Users \\ abc \\ error.pl”,我会看到/获得预期的错误级别1,这是什么问题?

Several points here. 这里有几点。 First of all, you have not shown us the code where you "invoke the perl script", but the code where you invoke portableshell.bat file, and you did not show us the contents of such Batch file . 首先,你还没有告诉我们的代码,你“调用perl脚本”,但如果你调用代码portableshell.bat文件,而你并没有告诉我们这样的批处理文件的内容。 So the first and simplest way to solve your problem is: 因此,解决问题的第一种也是最简单的方法是:

1- Insert in your windows Batch file the same command you use to "invoke the perl script" in the C:\\Strawberry32\\portableshell.bat file. 1-在Windows批处理文件中插入与C:\\Strawberry32\\portableshell.bat文件中的“调用perl脚本” 相同的命令 This will give access to the errorlevel from the perl script for sure. 这将确保可以从perl脚本访问错误级别。

I assume that the contents of portableshell.bat file are basically two lines: the one that run the perl script followed by this line: 我假设portableshell.bat文件的内容基本上是两行:运行perl脚本的那一行,其后是这一行:

exit /B %errorlevel%

The purpose of previous line is to return the errorlevel from the perl script to the Batch file that called this one Batch file. 上一行的目的是将错误级别从perl脚本返回到调用此一个批处理文件的批处理文件。 This mechanism works perfectly as long as this Batch file be called via call command : 只要通过 call 命令 call 此批处理文件,此机制就可以完美运行:

2- Invoke the C:\\Strawberry32\\portableshell.bat file via a call command: 2-通过call命令调用C:\\Strawberry32\\portableshell.bat文件:

call C:\Strawberry32\portableshell.bat C:\Users\abc\error.pl

If the portableshell.bat file is invoked via cmd /c instead of call , then you must set the returning errorlevel of cmd.exe program via a plain exit command, with NO /B switch: 如果通过cmd /c而不是call调用了portableshell.bat文件,则必须使用NO /B开关通过简单的exit命令设置cmd.exe程序的返回错误级别:

3- Invoke the C:\\Strawberry32\\portableshell.bat file via cmd /c line, but terminate it with this command: 3-通过cmd /c行调用C:\\Strawberry32\\portableshell.bat文件,但使用以下命令终止它:

exit %errorlevel%

You may read a further explanation of the errorlevel stuff and the differences of exit /B number vs exit number at What are the ERRORLEVEL values set by internal cmd.exe commands? 您可以在以下位置阅读有关错误级别内容以及exit /B numberexit number的区别的进一步说明: 内部cmd.exe命令设置的ERRORLEVEL值是多少? . At that site you may read that a start /B /WAIT portableshell.bat command may also be used to solve this problem, but this method also requires to end the .bat file with exit %errorlevel% in the same way as the cmd /c method. 在该站点上,您可能会读到start /B /WAIT portableshell.bat命令也可以用来解决此问题,但是此方法还需要以与exit %errorlevel% cmd /c相同的方式以exit %errorlevel%结束.bat文件。 cmd /c方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM