CALL命令与带/WAIT选项的START命令的区别

182

如何使用带有WAIT选项的START命令

START /wait notepad.exe 
START /wait  notepad.exe 

...使用RETURN命令有什么不同于使用CALL命令吗?

CALL notepad.exe 
CALL notepad.exe 

执行的任务不同,是否存在一个情况,一个人的行为可能会与另一个人不同?


3
请查看https://technet.microsoft.com/en-us/library/bb491005.aspx,其中涉及START,而https://technet.microsoft.com/en-us/library/bb490873.aspx则涉及CALL。 - NoWar
5个回答

228

对于exe文件,我认为差异几乎不重要。
但是要启动exe甚至不需要CALL

当启动另一个批处理文件时,这是一个很大的区别,
因为CALL会在同一个窗口中启动它,并且被调用的批处理文件可以访问相同的变量上下文。
因此,它也可以更改影响调用者的变量。

START将为被调用的批处理文件创建一个新的cmd.exe,并且在没有/b的情况下,它将打开一个新窗口。
由于这是一个新的上下文,变量无法共享。

区别

使用start /wait <prog>
- 当<prog>结束时,环境变量的更改会丢失
- 调用者会等待,直到<prog>完成

使用call <prog>
- 对于exe文件,它可以省略,因为它等同于只启动<prog>
- 对于一个exe-prog,调用者批处理文件会等待或异步启动exe,但其行为取决于exe本身。
- 对于batch文件,当被调用的<batch-file>完成时,调用者批处理文件将继续执行,WITHOUT call不会将控制返回给调用者批处理文件

补充说明:

使用CALL可以更改参数(对于批处理和exe文件),但只有当它们包含插入符或百分号时。

call myProg param1 param^^2 "param^3" %%path%%

将会被扩展为(在批处理文件内部)

myProg param1 param2 param^^3 <content of path>

35
使用 START /WAIT 执行 file.bat 时,需要指定 START /WAIT cmd /c "file.bat",而不仅仅是 START /WAIT "file.bat",否则为 file.bat 创建的 cmd 窗口将保持打开状态。 - FrinkTheBrave
6
你可以在以下链接中找到CALL和START的比较:https://ss64.com/nt/start.html(已更新,包含“Start /Wait”和“START vs CALL”部分) - Alfredo Capobianchi
2
我最喜欢的是 start /wait /b cmd /c <batchfile.bat>,因为批处理文件会在同一个命令窗口中依次运行。 - linux64kb
@linux64kb,但对于批处理文件来说并不是必需的,你只需要使用call batchfile.bat - jeb
1
"setlocal" 不是为此而设计的吗? - il--ya
显示剩余3条评论

20

我认为它们应该表现得大致相同,但还是有一些区别。START通常用于启动应用程序或启动给定文件类型的默认应用程序。这样,如果你输入START http://mywebsite.com,它就不会执行START iexplore.exe http://mywebsite.com

START myworddoc.docx将启动Microsoft Word并打开myworddoc.docx。 CALL myworddoc.docx也能实现同样的功能……但是START提供了更多的选项来设置窗口状态和其他属性。它还允许设置进程优先级和亲和性。

简而言之,由于START提供了额外的选项,因此应该选择它作为您的工具。

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
  [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
  [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
  [command/program] [parameters]

"title"     Title to display in window title bar.
path        Starting directory.
B           Start application without creating a new window. The
            application has ^C handling ignored. Unless the application
            enables ^C processing, ^Break is the only way to interrupt
            the application.
I           The new environment will be the original environment passed
            to the cmd.exe and not the current environment.
MIN         Start window minimized.
MAX         Start window maximized.
SEPARATE    Start 16-bit Windows program in separate memory space.
SHARED      Start 16-bit Windows program in shared memory space.
LOW         Start application in the IDLE priority class.
NORMAL      Start application in the NORMAL priority class.
HIGH        Start application in the HIGH priority class.
REALTIME    Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE        Specifies the preferred Non-Uniform Memory Architecture (NUMA)
            node as a decimal integer.
AFFINITY    Specifies the processor affinity mask as a hexadecimal number.
            The process is restricted to running on these processors.

            The affinity mask is interpreted differently when /AFFINITY and
            /NODE are combined.  Specify the affinity mask as if the NUMA
            node's processor mask is right shifted to begin at bit zero.
            The process is restricted to running on those processors in
            common between the specified affinity mask and the NUMA node.
            If no processors are in common, the process is restricted to
            running on the specified NUMA node.
WAIT        Start application and wait for it to terminate.

12

当调用regsvr32.exe /s时,callstart /wait之间存在一个有用的区别,这也是由Gary在他回答的how-do-i-get-the-application-exit-code-from-a-windows-command-line中提到的。

call regsvr32.exe /s broken.dll
echo %errorlevel%

将始终返回0,但是

start /wait regsvr32.exe /s broken.dll
echo %errorlevel%

将返回regsvr32.exe的错误级别。


9
以下是我在同时运行批处理文件(使用不同的输入参数同时运行相同的bat文件)时发现的内容:
假设你有一个名为LongRunningTask.exe的exe文件,它执行长时间任务。
如果你直接从bat文件调用exe文件,只有第一次调用LongRunningTask会成功,而其余的都会得到操作系统错误“文件已被进程使用”。
如果你使用这个命令:
start /B /WAIT "" "LongRunningTask.exe" "parameters"
你将能够运行多个实例的bat和exe,同时在任务完成之前等待批处理继续执行剩余的命令。/B选项是为了避免创建另一个窗口,空引号是必需的,以使命令正常工作,请参见下面的参考文献。
请注意,如果你不在start中使用/WAIT,LongRunningTask将与批处理文件中的其余命令同时执行,所以如果这些命令中的一个需要LongRunningTask的输出,可能会导致问题。
总之:
这不能并行运行:
call LongRunningTask.exe 这将并行运行,只要命令输出与批处理文件的其余部分没有数据依赖关系即可:
  • start /B "" "LongRunningTask.exe" "parameters"
这将并行运行并等待任务完成,因此您可以使用输出:
  • start /B /WAIT "" "LongRunningTask.exe" "parameters"
start 命令的参考资料:如何在不离开程序的情况下从批处理文件中运行程序?

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接