链接批处理脚本时出现意外的双“&"号/管道行为 [英] unexpected double ampersand/pipe behavior when chaining batch script

查看:83
本文介绍了链接批处理脚本时出现意外的双“&"号/管道行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的foo.bat文件:

My foo.bat file:

exit /b 1

我在cmd提示符下执行的操作:

What I execute in a cmd prompt:

foo.bat && echo "OK"

结果:

exit /b 1
"OK"

但是,当我使用双管道时,不会发生回声:

Yet, when I use double pipe, the echo doesn't occur:

foo.bat || echo "OK"

结果:

exit /b 1

这与我期望的完全相反的行为和||去做.请参见 https://ss64.com/nt/call.html ,其中说:

This is the exact opposite behavior of what I expect && and || to do. See https://ss64.com/nt/call.html, where it says:

commandA&& commandB运行commandA,如果成功,则运行commandB

commandA && commandB Run commandA, if it succeeds then run commandB

commandA || commandB运行commandA,如果失败,则运行commandB

commandA || commandB Run commandA, if it fails then run commandB

我失去理智了吗?我在这里想念什么?

Am I losing my mind? What am I missing here?

推荐答案

||&&响应上一条命令(左侧最后执行的命令)的返回代码.无论上下文如何,所有程序都将以错误代码退出.

|| and && respond to the return code of the previous command (the last executed command on the left). All programs exit with an error code, regardless of context.

EXIT /B 1设置批处理ERRORLEVEL,严格来说是cmd.exe概念.

EXIT /B 1 sets the batch ERRORLEVEL, which is strictly a cmd.exe concept.

返回代码和ERRORLEVEL是 相同的东西!

The return code and ERRORLEVEL are not the same thing!

当执行批处理文件时,仅当通过CALL执行了批处理文件时,才将返回的ERRORLEVEL作为返回代码返回.

When a batch file is executed, the exiting ERRORLEVEL is only returned as the return code if the batch file was executed via CALL.

在不执行CALL的情况下执行批处理文件时,&&||会响应脚本中最后执行的命令.

When a batch file is executed without CALL, && and || respond to the last command executed within the script.

EXIT /B 1将ERRORLEVEL设置为1,但是命令执行成功,因此返回码为0.

EXIT /B 1 sets ERRORLEVEL to 1, but the command executed successfully, so the return code is 0.

使用CALL时,CALL命令在脚本终止后查看ERRORLEVEL,并将返回码设置为ERRORLEVEL.

When CALL is used, the CALL command looks at the ERRORLEVEL after the script terminates, and sets the return code to the ERRORLEVEL.

这篇关于链接批处理脚本时出现意外的双“&"号/管道行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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