windows CALL 命令与带有 /WAIT 选项的 START
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13257571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CALL command vs. START with /WAIT option
提问by Chad
How is the START command with a WAIT option
带有 WAIT 选项的 START 命令如何
START /wait notepad.exe
START /wait notepad.exe
...any different from using a CALL command?
...与使用 CALL 命令有什么不同?
CALL notepad.exe
CALL notepad.exe
Is there a situation where one may behave differently that the other dependending on what is being executed?
是否存在一种情况,根据正在执行的内容,一个人的行为可能与另一个人不同?
回答by jeb
For exefiles, I suppose the differences are nearly unimportant.
But to start an exeyou don't even need CALL
.
对于exe文件,我认为差异几乎不重要。
但是要启动一个exe,你甚至不需要CALL
.
When starting another batch it's a big difference,
as CALL
will start it in the same window and the called batch has access to the same variable context.
So it can also change variables which affects the caller.
当启动另一个批处理时,这是一个很大的不同,
因为CALL
它将在同一个窗口中启动它并且被调用的批处理可以访问相同的变量上下文。
所以它也可以改变影响调用者的变量。
START
will create a new cmd.exe for the called batch and without /b it will open a new window.
As it's a new context, variables can't be shared.
START
将为被调用的批处理创建一个新的 cmd.exe,没有 /b 它将打开一个新窗口。
由于它是一个新的上下文,因此不能共享变量。
Differences
差异
Using start /wait <prog>
- Changes of environment variables are lost when the <prog>
ends
- The caller waits until the <prog>
is finished
使用start /wait <prog>
- 环境变量的更改在<prog>
结束时丢失
- 调用者等待直到<prog>
完成
Using call <prog>
- For exeit can be ommited, because it's equal to just starting <prog>
- For an exe-progthe caller batch waits or starts the exeasynchronous, but the behaviour depends on the exeitself.
- For batchfiles, the caller batch continues, when the called <batch-file>
finishes, WITHOUT call the control will not return to the caller batch
使用call <prog>
- 对于exe可以省略,因为它等于刚刚启动<prog>
- 对于exe-prog,调用方批处理等待或异步启动exe,但行为取决于exe本身。
- 对于批处理文件,调用者批处理继续,当调用<batch-file>
完成时,WITHOUT call控件不会返回到调用者批处理
Addendum:
附录:
Using CALL
can change the parameters (for batch and exe files), but only when they contain carets or percent signs.
使用CALL
可以更改参数(对于批处理和 exe 文件),但仅当它们包含插入符号或百分号时。
call myProg param1 param^^2 "param^3" %%path%%
Will be expanded to (from within an batch file)
将扩展为(从批处理文件中)
myProg param1 param2 param^^3 <content of path>
回答by mckeejm
I think that they should perform generally the same, but there are some differences.
START
is generally used to start applications or to start the default application for a given file type. That way if you START http://mywebsite.com
it doesn't do START iexplore.exe http://mywebsite.com
.
我认为它们的性能应该大致相同,但也存在一些差异。
START
通常用于启动应用程序或启动给定文件类型的默认应用程序。这样,如果你START http://mywebsite.com
不这样做START iexplore.exe http://mywebsite.com
。
START myworddoc.docx
would start Microsoft Word and open myworddoc.docx.CALL myworddoc.docx
does the same thing... however START
provides more options for the window state and things of that nature. It also allows process priority and affinity to be set.
START myworddoc.docx
将启动 Microsoft Word 并打开 myworddoc.docx。CALL myworddoc.docx
做同样的事情......但是START
为窗口状态和这种性质的事情提供了更多的选择。它还允许设置进程优先级和关联性。
In short, given the additional options provided by start, it should be your tool of choice.
简而言之,考虑到 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.
回答by aedjp
There is a useful difference between call
and start /wait
when calling regsvr32.exe /s
for example, also referenced by Gary in
in his answer to how-do-i-get-the-application-exit-code-from-a-windows-command-line
例如,在调用之间call
和start /wait
调用时有一个有用的区别regsvr32.exe /s
,Gary在他对how-do-i-get-the-application-exit-code-from-a-windows-command-line 的回答中也引用了这一点
call regsvr32.exe /s broken.dll
echo %errorlevel%
will always return 0 but
总是会返回 0 但
start /wait regsvr32.exe /s broken.dll
echo %errorlevel%
will return the error level from regsvr32.exe
将从 regsvr32.exe 返回错误级别
回答by Cherno
This is what I found while running batch files in parallel (multiple instances of the same bat file at the same time with different input parameters) :
这是我在并行运行批处理文件时发现的(同一 bat 文件的多个实例同时具有不同的输入参数):
Lets say that you have an exe file that performs a long task called LongRunningTask.exe
假设您有一个执行名为 LongRunningTask.exe 的长任务的 exe 文件
If you call the exe directly from the bat file, only the first call to the LongRunningTask will succed, while the rest will get an OS error "File is already in use by the process"
如果直接从 bat 文件中调用 exe,则只有第一次调用 LongRunningTask 会成功,而其余的会得到操作系统错误“文件已被进程使用”
If you use this command:
如果您使用此命令:
start /B /WAIT "" "LongRunningTask.exe" "parameters"
start /B /WAIT "" "LongRunningTask.exe" "参数"
You will be able to run multiple instances of the bat and exe, while still waiting for the task to finish before the bat continues executing the remaining commands. The /B option is to avoid creating another window, the empty quotes are needed in order to the command to work, see the reference below.
您将能够运行 bat 和 exe 的多个实例,同时在 bat 继续执行剩余命令之前仍在等待任务完成。/B 选项是为了避免创建另一个窗口,需要空引号才能使命令工作,请参阅下面的参考。
Note that if you don′t use the /WAIT in the start, the LongRunningTask will be executed at the same time than the remaining commands in the batch file, so it might create problems if one of these commands requires the output of the LongRunningTask
请注意,如果您在启动时不使用 /WAIT,则 LongRunningTask 将与批处理文件中的其余命令同时执行,因此如果这些命令之一需要 LongRunningTask 的输出,则可能会产生问题
Resuming :
恢复:
This can′t run in parallel :
这不能并行运行:
- call LongRunningTask.exe
- 调用 LongRunningTask.exe
This will run in parallel and will be ok as far as there are no data dependencies between the output of the command and the rest of the bat file :
这将并行运行,只要命令的输出和 bat 文件的其余部分之间没有数据依赖关系就可以了:
- start /B "" "LongRunningTask.exe" "parameters"
- start /B "" "LongRunningTask.exe" "参数"
This will run in parallel and wait for the task to finish, so you can use the output :
这将并行运行并等待任务完成,因此您可以使用输出:
- start /B /WAIT "" "LongRunningTask.exe" "parameters"
- start /B /WAIT "" "LongRunningTask.exe" "参数"
Reference for the start command : How can I run a program from a batch file without leaving the console open after the program start?
启动命令的参考:如何在程序启动后不让控制台保持打开状态从批处理文件运行程序?
回答by Developer
Call
称呼
Calls one batch program from another without stopping the parent batch program.The call command accepts labels as the target of the call. Call has no effect at the command-line when used outside of a script or batch file. https://technet.microsoft.com/en-us/library/bb490873.aspx
在不停止父批处理程序的情况下从另一个调用一个批处理程序。call 命令接受标签作为调用的目标。在脚本或批处理文件之外使用时,调用在命令行中无效。 https://technet.microsoft.com/en-us/library/bb490873.aspx
Start
开始
Starts a separate Command Prompt windowto run a specified program or command. Used without parameters, start opens a second command prompt window. https://technet.microsoft.com/en-us/library/bb491005.aspx
启动单独的命令提示符窗口以运行指定的程序或命令。不带参数使用时,start 会打开第二个命令提示符窗口。 https://technet.microsoft.com/en-us/library/bb491005.aspx
回答by Steven Sheldon
This is an old thread, but I have just encountered this situation and discovered a neat way around it. I was trying to run a setup.exe, but the focus was returning to the next line of the script without waiting for the setup.exe to finish. I tried the above solutions with no luck.
这是一个旧线程,但我刚刚遇到这种情况并发现了一种巧妙的方法。我试图运行 setup.exe,但焦点返回到脚本的下一行,而无需等待 setup.exe 完成。我尝试了上述解决方案但没有运气。
In the end, piping the command through more did the trick.
最后,通过 more 来传递命令就成功了。
setup.exe {arguments} | more
setup.exe {参数} | 更多的