我知道如果你有两个.bat或.cmd文件,我们称它们为foo和bar ,以下规则适用:
没有call :
:: Welcome to foo.bat @bar.bat @echo We never get to this line because bar.bat is "chain-executed".
随着call :
:: Welcome to foo.bat @call bar.bat @echo This line is executed after bar.bat returns.
我的问题是:是否有一种方法来执行逆向操作,即确保非batch file可执行文件被链接?
遍历所有的环境variables,并根据前缀采取行动
Windows 7:使用应用程序path与batch file运行应用程序
在cmd(bat文件)中运行reg命令?
如何将BAT文件中的命令发送到Windows中正在运行的NodeJS进程?
集中批量启动的应用程序
:: Welcome to foo.bat @chain bar.exe @echo Even though bar is Now an .exe,we never get to this line. @echo At least,that would be the case if the "chain" command really existed.
换句话说,在最后一个例子中,是否有一种执行虚构chain命令的function?
如何从batch file写入Windowsnetworking共享?
batch file通过迭代将文件复制到多台计算机?
在后台使用plink转发本地端口,并在本地机器上执行命令
如何将批处理脚本中的每个循环迭代发送到新的cmd窗口并继续循环
将string转换为batch file中的整数
有必要使用命令start在单独的进程中运行可执行文件,并且另外退出当前批处理或整个命令进程。
@echo off echo Welcome to %~nx0 start "Title" bar.exe & exit /B echo Even though bar is Now an .exe,we never get to this line.
这个批处理文件在一个单独的进程中启动bar.exe ,如果可执行文件是Title作为窗口标题,则是在这种情况下打开的新控制台窗口的控制台应用程序。
然后在start完成后exit /B被命令处理器无条件地执行,而bar.exe在一个单独的进程中运行,导致当前批处理文件的终止处理。
如果此批处理文件不是使用命令调用从另一个批处理文件调用的 ,那么命令处理器现在将完成处理批处理文件,从而导致退出命令处理,除了使用cmd.exe调用批处理文件并使用/K来保持命令提示符窗口在完成批处理后打开,默认情况下不是这样。
但是,如果这个批处理文件是从另一个批处理文件调用的 ,那么这个子批处理文件的处理就完成了,命令处理器继续处理父批处理文件,而bar.exe运行在一个单独的进程中。
@echo off echo Welcome to %~nx0 start "Title" bar.exe & exit echo Even though bar is Now an .exe,we never get to this line.
在这个批处理代码中,命令出口没有选项/B ,即使当前的批处理文件是通过调用另一个批处理文件调用的,并且即使批处理文件的处理是在一个单独的进程中start完成后, bar.exe导致终止命令处理用带参数/K cmd.exe启动。
而不是无条件地连接两个命令开始和退出与运算符&也可以使用一个块如下所示的两个变种。
只需退出当前的批处理:
@echo off echo Welcome to %~nx0 ( start "Title" bar.exe exit /B ) echo Even though bar is Now an .exe,we never get to this line.
退出整个命令过程:
@echo off echo Welcome to %~nx0 ( start "Title" bar.exe exit ) echo Even though bar is Now an .exe,we never get to this line.
退出当前批处理或整个命令处理的应用程序的这种启动当然仅在bar.exe被启动时才有意义,这取决于批处理文件中的至少一个条件。
注1:
也可以使用goto :EOF来代替exit /B来结束当前批处理。
注2:如果命令是批次子程序的一部分,也就是说,由于批处理子程序就像一个子批处理文件一样,所以在call :label下面的代码时, goto :EOF和exit /B结果都会退出子程序在关于批处理的主批文件内。
一些更多示例来演示call和exit /B的行为:
Test1.bat :
@echo off echo Running %~nx0 call Test2.bat echo Finished %~nx0
Test2.bat :
@echo off echo Running %~nx0 Test3.bat echo Finished %~nx0
Test3.bat :
@echo off echo Finished %~nx0
Running Test1.bat Running Test2.bat Finished Test3.bat Finished Test1.bat
因此,行Finished Test2.bat丢失,因为命令处理Test3.bat直接从Test3.bat返回到Test1.bat 。
接下来我们编译下面的C代码到控制台应用程序Test.exe :
#include <stdio.h> int main (int argc,char* argv[]) { if(argc > 1) { printf("Running %s with argument %sn",argv[0],argv[1]); } else { printf("Running %s without an argumentn",argv[0]); } return 0; }
我们在以下两个批处理文件中使用Test.exe :
Test4.bat :
@echo off echo Running %~nx0 Test.exe 4 call Test5.bat echo Finished %~nx0
Test5.bat :
@echo off echo Running %~nx0 Test.exe 5 Test.exe 6 & exit /B echo Finished %~nx0
Running Test4.bat Running Test.exe with argument 4 Running Test5.bat Running Test.exe with argument 5 Running Test.exe with argument 6 Finished Test4.bat
因此,行Finished Test5.bat丢失,因为命令处理程序从执行Test.exe与参数6直接返回到Test4.bat 。
但是,如果bar是bat或cmd文件扩展名的批处理文件,或者是exe或com文件扩展名的可执行文件,那么使用bar & exit /B是非常重要的。 这可以通过将Test2.bat代码Test2.bat为:
@echo off echo Running %~nx0 Test3.bat & exit /B echo Finished %~nx0
Running Test1.bat Running Test2.bat Finished Test3.bat
因此,在第二个批处理文件中附加了exit /B ,命令处理器将第二批处理文件中的退出解释为第一个批处理文件的上下文中的退出。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。