为什么不需要`call`从管道中涉及的被调用的批处理脚本返回? [英] Why is there no need for `call` to return from called batch script which is involved in a pipe?

查看:260
本文介绍了为什么不需要`call`从管道中涉及的被调用的批处理脚本返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个执行另一个批处理文件(callee)的批处理文件(调用程序),需要使用调用命令,以便在callee完成执行。以下是一个示例:

Supposing there is a batch file (caller) which executes another batch file (callee), the call command needs to be used in order to return to the caller after the callee finishes execution. Here is an example:

caller.bat
$ b

caller.bat:

echo Calling another script...
call callee.bat
echo Returned from callee...

callee.bat 位置)

echo   Being called from caller...

输出将是这个(忽略命令echos),显示执行按预期返回:

The output will be this (omitting the command echos), showing that execution returned as expected:


Calling another script...
  Being called from caller...
Returned from callee...


如果调用命令在调用者中被忽略,输出将是:

If the call command was dismissed in the caller, the output would be:


Calling another script...
  Being called from caller...







但是一旦被调用程序涉及到管道( | ),那么 call 命令。例如:


But as soon as the callee is involved in a pipe (|), there is no difference in whether or not the call command is used. For instance:

caller.bat (被调用者保持不变)

echo Calling another script...
break | callee.bat
echo Returned from callee...

输出将是这样,没有调用命令。

The output will be this, although there is no call command.


Calling another script...
  Being called from caller...
Returned from callee...


这种行为的原因是什么导致执行在这里返回调用者?

What is the reason for this behaviour, what causes execution to return to the caller here?

推荐答案

有两种方法从调用者( main 文件)调用另一个批处理文件: call callee.bat cmd / C callee.bat ;区别在于调用在调用程序的同一上下文中执行另一个批处理文件,因此它们共享相同的环境变量和另一个状态,而 cmd / C 在完全分开的上下文中执行另一个批处理文件。就像个人说明一样,我用来命名内部子程序通过调用外部子程序调用的批处理文件通过 cmd / C (和覆盖直接调用的批处理文件而不调用调用 cmd / C 继承调用程序批处理文件的行为和上下文)。

There are two ways to call another Batch file from the caller one (main file): call callee.bat and cmd /C callee.bat; the difference is that call execute the other Batch file in the same context of the caller program, so they share the same environment variables and another status, whereas cmd /C execute the other Batch file in an entirely separated context. Just as a personal note, I used to name internal subroutine the Batch file invoked via call, and external subroutine the one invoked via cmd /C (and overlay the Batch file directly invoked without call nor cmd /C, that inherits the behavior and context of the caller Batch file).

在管道的执行中,管道的两侧都通过 cmd / C 执行,因此两侧都作为外部子程序调用。这种方式,如果管道的任何一边是一个Batch.BAT文件,当它结束时返回到调用程序。

In the execution of a pipe, both sides of the pipe are executed via a cmd /C, so both sides are invoked as external subroutines. This way, if any side of a pipe is a Batch.BAT file, it returns to the caller program when it ends.

在被调用者批处理文件放置在中的/ F 命令,以及exaclty也是同样的原因; for / F %% a in('calle.bat')do ...

The same behavior happen in a callee Batch file placed in a for /F command, and exaclty for the same reason; for /F %%a in ('calle.bat') do ...

这篇关于为什么不需要`call`从管道中涉及的被调用的批处理脚本返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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