本帖最后由 CrLf 于 2015-3-25 01:00 编辑
回复 7# bailong360
回复 8# shootman2
仔细想一下其实不需要交换句柄 1 和 2,化简一下:(外交部发言人 for /f 代表党和人民热烈欢迎英勇突围的 %errorlevel%,并致以最诚挚的问候) | for /f "delims=" %%a in ('"(call myCommand.bat 2>nul & call echo %%errorlevel^^%% 1>&2)|mtee /d /t /+ sysLog.log 2>&1"') do ( | | echo call myCommand.bat 的退出码是 %%a | | )COPY |
----------------------------------------------------------------------------------------------------
经过预处理变成这样:(for /f 欢迎突围的 %errorlevel%) | for /f "delims=" %a in ('"(call myCommand.bat 2>nul & call echo %errorlevel^^% 1>&2)|mtee /d /t /+ sysLog.log 2>&1"') do ( | | echo call myCommand.bat 的退出码是 %a | | )COPY |
----------------------------------------------------------------------------------------------------
for /f 隐含了一个 cmd,执行起来相当于:(这里没发生什么,只是在句柄 1 中包含了 %errorlevel% 的内容)cmd /c "(call myCommand.bat 2>nul & call echo %errorlevel^^% 1>&2)|mtee /d /t /+ sysLog.log 2>&1"COPY ----------------------------------------------------------------------------------------------------
管道前的代码块触发了一个内层的 cmd,外面的 cmd 执行的代码相当于这样:(因为 mtee 已经取走了所有来自句柄 1 的输出,所以句柄 1 为空,可以放心将句柄 2 再重定向到句柄 1)(cmd /c "(call myCommand.bat 2>nul & call echo %errorlevel^^% 1>&2)"|mtee /d /t /+ sysLog.log) 2>&1COPY ----------------------------------------------------------------------------------------------------
里面那个隐含的 cmd 执行的是这样:(把这句柄 1 的输出都交给 mtee 进行处理,句柄 2 的未被读取)cmd /c (call myCommand.bat 2>nul & call echo %errorlevel^% 1>&2)COPY ----------------------------------------------------------------------------------------------------
管道前的部分执行的是这样:(先将 call myCommand.bat 的句柄 1 输出到句柄 2 中,并屏蔽原有的句柄 2 输出,再输出其 %errorlevel% 到正常的句柄 2) | ( | | call myCommand.bat 2>nul | | call echo %errorlevel% 1>&2 | | ) | | COPY |
----------------------------------------------------------------------------------------------------
接下来,请按 ctrl+A 全选变成蓝底白字,然后从下往上看粗体字 |