英文:
Batch file %errorlevel% malfunction with `choice` command
问题
以下是翻译好的部分:
我有一个批处理文件中的菜单,由choice命令驱动:
choice /C PNQTF /N /M "选择: "
echo Errorlevel is %errorlevel%
if %errorlevel%==5 goto ViewFTP
if %errorlevel%==4 goto AutoTransferToggle
if %errorlevel%==3 goto TheEnd
if %errorlevel%==2 goto InternalLink
if %errorlevel%==1 goto NewSub
goto begin
20次中有19次没有问题。菜单运行得很好。但在很小的百分比情况下,当我按下 `Q` 退出时,代码似乎随机跳到了 `:NewSub`,即使 `Errorlevel is %errorlevel%` 语句在屏幕上回显 `Errorlevel is 3`... 但它仍然按照错误级别为1的方式执行指令!
我考虑过也许使用 `!errorlevel!` 以确保安全,但这并不重要,因为这组选择不在任何代码块中 - 它不包含在任何 `if` 语句或任何函数中。我会说,如果我只是运行批处理文件然后立即退出,这个问题从来不会发生... 在批处理文件的黑暗深处似乎有一些东西在通过 `goto begin` 返回到开始时仍然能够停留和困扰菜单。
由于这个问题导致我拔掉了很多头发,我已经失去了一半的头发。胸毛可能是下一个受害者。唯一看起来比这更随机的是...
英文:
I have a menu in a batch file that is powered by the choice command:
choice /C PNQTF /N /M "Choice: "
echo Errorlevel is %errorlevel%
if %errorlevel%==5 goto ViewFTP
if %errorlevel%==4 goto AutoTransferToggle
if %errorlevel%==3 goto TheEnd
if %errorlevel%==2 goto InternalLink
if %errorlevel%==1 goto NewSub
goto begin
19 times out of 20, there are no problems. The menu works fine. But a small percentage of the time, when I push Q
to quit, the code seems to randomly skip all the way to :NewSub
even though the Errorlevel is %errorlevel%
statement echoes to the screen that Errorlevel is 3
... and yet it still follows the instructions as if errorlevel were 1!
I thought of maybe using !errorlevel!
just to be safe, but it doesn't matter because this set of choices is not in any block of code - it's not enclosed in any if
statements or in any functions. I will say this issue never happens if I just run the batch file and immediately quit... there is something in the dark depths of the batch file that is somehow managing to linger and haunt the menu when the execution returns back to the beginning via goto begin
.
I am missing half of the hair on my head because this issue has caused me to pull so much of it out. Chest hair might be next. The only thing that would seem more random than this
答案1
得分: 0
结果证明解决方案是我之前已经检查过的,但不够仔细。我追踪了我的代码,并发现一个情景,在这个情景中,用户在调用的函数中返回到主菜单(goto begin
),但该函数从未通过 goto :eof
语句结束。
只有当用户尝试从主菜单退出时才会出现症状。当用户选择 Q
(这会将他们带到文件的底部,到一个名为 :TheEnd
的标签),它会直接跳回到 goto begin
语句之后的行,这个语句中断了未完成的函数。用户看不到除了 "按了Q后,突然执行了这个其他的代码" 之外的任何输出。
所以,事实证明 errorlevel
工作得很好;将执行返回到该行代码只是让 errorlevel
看起来出了问题,而事实上批处理文件执行将未完成的函数视为最后才执行的任务清单项,然后最终停止运行。
英文:
It turns out the solution is something I had already checked for before, but not careful enough. I traced my code and found a scenario where the user is returned to the main menu (goto begin
) while in the middle of a called function - so the function was never concluded with a goto :eof
statement.
Symptoms only occur when the user tries to quit from the main menu. When the user selects Q
(which brings them to the very bottom of the file to a label called :TheEnd
), it skips right back up to the line after the goto begin
statement that interrupted the unconcluded function. The user doesn't see any output other than "after I hit Q, suddenly it's executing this other code."
So, as it turns out, errorlevel
was working just fine; the returning of execution back to that line of code only made it appear that errorlevel
was messing up, when in fact the batch file execution was treating the non-completed function like a bucket list item before finally dying.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论