|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『楼 主』:
ERRORLEVEL请教?
使用 LLM 解释/回答一下
我想问一下ERRORLEVEL是命令执行后的返回退出值,这具体怎么解释啊。什么意思啊?譬如call set "c=%%a:00-14-78-42-93-7d=*%%" 这句命令有退出值吗?是多少呢?
I want to ask that ERRORLEVEL is the return exit value after a command is executed. How exactly is this explained? What does it mean? For example, the command "call set "c=%%a:00-14-78-42-93-7d=*%%"", does this command have an exit value? What is it?
|
|
2007-10-4 21:39 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
|
2007-10-4 21:42 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
echo off
dir z:
rem 如果退出代码为1(不成功)就跳至标题1处执行
IF ERRORLEVEL 1 goto 1
rem 如果退出代码为0(成功)就跳至标题0处执行
IF ERRORLEVEL 0 goto 0
:0
echo 命令执行成功!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:1
echo 命令执行失败!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:exit
Rem 这里是程序的出口
这段代码执行了dir z:后返回一个什么值呢就是0或1吗那么是不是每条命令执行后都会返回一个退回值呢?
```batch
echo off
dir z:
rem 如果退出代码为1(不成功)就跳至标题1处执行
IF ERRORLEVEL 1 goto 1
rem 如果退出代码为0(成功)就跳至标题0处执行
IF ERRORLEVEL 0 goto 0
:0
echo 命令执行成功!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:1
echo 命令执行失败!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:exit
Rem 这里是程序的出口
这段代码执行了dir z:后返回一个什么值呢就是0或1吗那么是不是每条命令执行后都会返回一个退回值呢?
```
If the `dir z:` command finds the specified drive (here, drive Z:), it returns 0; if it doesn't find the drive, it returns 1. And yes, most commands in batch scripting return an exit code (also known as a return value), where 0 typically indicates success and a non-zero value indicates an error or failure.
|
|
2007-10-4 21:47 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
请高手指教!先道谢了。
Please ask experts for guidance! Thank you in advance.
|
|
2007-10-4 21:48 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
用%ERRORLEVEL%来查看上个命令的返回值。
通常%ERRORLEVEL%值为0表示命令成功执行完,若大于等于1,表示命令执行失败或者出错。
其它的请搜索论坛,以前很多讨论。
Use %ERRORLEVEL% to check the return value of the previous command.
Usually, a %ERRORLEVEL% value of 0 means the command executed successfully, and if it is greater than or equal to 1, it means the command failed or had an error.
For others, please search the forum, there were many discussions before.
|
|
2007-10-4 22:31 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
谢谢版主 !为什么我用set ERRORLEVEL 不能看它的返回值呢
Thanks moderator! Why can't I see its return value with set ERRORLEVEL?
|
|
2007-10-4 22:36 |
|
|
wudixin96
银牌会员
    
积分 1928
发帖 931
注册 2007-1-6
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
用echo %errorlevel%查看
Check with echo %errorlevel%
|
|
2007-10-4 22:48 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by wudixin96 at 2007-10-4 10:48 PM:
用echo %errorlevel%查看
谢谢你的指教我知道可以用echo %errorlevel% 看返回值但是我原先认为errorlevel是个变量应该可以用set errorlevel 来看它的值,但是实践证明并非如此
那我就不明白了errorlevel不是一个变量吗
Originally posted by wudixin96 at 2007-10-4 10:48 PM:
Check with echo %errorlevel%
Thank you for your instruction. I know that you can use echo %errorlevel% to view the return value. But I originally thought that errorlevel was a variable and should be able to use set errorlevel to view its value, but practice has proved that this is not the case.
Then I don't understand. Isn't errorlevel a variable?
|
|
2007-10-4 23:00 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
我看过一篇文章上面说它是系统变量啊 为什么像%systemdrive%的能显示但是set errorlevel不能显示呢?
I have read an article saying it's a system variable. Why can something like %systemdrive% be displayed but set errorlevel can't be displayed?
|
|
2007-10-4 23:03 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
|
2007-10-4 23:12 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Re wordexport:
可以这么理解吧:
set中的部分变量都可以叫做“环境变量”,“环境变量”的主要作用是在程序运行前提供一些可供利用的环境选项,这些不是在程序运行之后才设定的,而是之前就设定好的,它们的值通常是不变化的。
而errorlevel可以理解成程序或者命令运行之后的一个返回值(ExitCode),通过这个返回值可以粗略判断程序的运行状态(是正常执行还是有错误出现),因此这个值是随着执行的命令的变化而变化的,就因这些和环境变量的不同,所以你set errorlevel得不到它的值吧。至于到底为什么,我也不知道,微软知道。
还有一个建议就是,判断程序的返回值时,最好是用“if errorlevel ExitCode”的格式,因为errorlevel还可以被定义成一个变量,此时如果用%Errorlevel%来判断的话,就不是ExitCode了,而是你定义的变量%errorlevel%的值了,这样就会导致判断错误而使程序流程“走偏”。
Re wordexport:
It can be understood like this:
Part of the variables in set can all be called "environment variables". The main function of "environment variables" is to provide some available environment options before the program runs. These are not set after the program runs, but are set in advance, and their values usually do not change.
And errorlevel can be understood as a return value (ExitCode) after a program or command runs. Through this return value, you can roughly judge the running status of the program (whether it is executed normally or an error occurs). Therefore, this value changes with the executed command. Because of these differences from environment variables, so you can't get its value by set errorlevel. As for why exactly, I don't know, Microsoft knows.
Another suggestion is that when judging the return value of a program, it is best to use the format of "if errorlevel ExitCode". Because errorlevel can also be defined as a variable. At this time, if you use %Errorlevel% to judge, it will not be ExitCode, but the value of the variable %errorlevel% you defined. This will cause the judgment to be wrong and make the program process "go astray".
|
|
2007-10-5 00:24 |
|
|
knoppix7
银牌会员
    
积分 1287
发帖 634
注册 2007-5-2 来自 cmd.exe
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by wordexport at 2007-10-4 10:36 PM:
谢谢版主 !为什么我用set ERRORLEVEL 不能看它的返回值呢
SET得帮助写的很明白:
如果命令扩展名被启用,有几个动态环境变量可以被扩展,但
不会出现在 SET 显示的变量列表中。每次变量数值被扩展时,
这些变量数值都会被动态计算。如果用户用这些名称中任何
一个定义变量,那个定义会替代下面描述的动态定义:
%CD% - 扩展到当前目录字符串。
%DATE% - 用跟 DATE 命令同样的格式扩展到当前日期。
%TIME% - 用跟 TIME 命令同样的格式扩展到当前时间。
%RANDOM% - 扩展到 0 和 32767 之间的任意十进制数字。
%ERRORLEVEL% - 扩展到当前 ERRORLEVEL 数值。
%CMDEXTVERSION% - 扩展到当前命令处理器扩展名版本号。
%CMDCMDLINE% - 扩展到调用命令处理器的原始命令行。
Originally posted by wordexport at 2007-10-4 10:36 PM:
Thank you, moderator! Why can't I see the return value when I use set ERRORLEVEL?
SET's help is very clear:
If command extensions are enabled, there are several dynamic environment variables that can be expanded, but they do not appear in the variable list displayed by SET. Each time the variable value is expanded, these variable values are dynamically calculated. If the user defines a variable with any of these names, that definition will replace the dynamic definition described below:
%CD% - Expands to the current directory string.
%DATE% - Expands to the current date in the same format as the DATE command.
%TIME% - Expands to the current time in the same format as the TIME command.
%RANDOM% - Expands to any decimal number between 0 and 32767.
%ERRORLEVEL% - Expands to the current ERRORLEVEL value.
%CMDEXTVERSION% - Expands to the current command processor extension version number.
%CMDCMDLINE% - Expands to the original command line that called the command processor.
|
|
2007-10-5 09:55 |
|
|
wordexport
初级用户
 
积分 87
发帖 42
注册 2007-9-27
状态 离线
|
|
2007-10-5 09:56 |
|