批处理脚本执行首个Asciidoctor命令后意外退出的问题
Asciidoctor执行后批处理脚本终止的解决方法
问题描述
我编写了一个批处理脚本,用于通过Asciidoctor构建文档,并借助Inno Setup生成安装程序。但发现执行Asciidoctor命令后,脚本会立即退出,后续命令无法执行;而调用iscc命令则可正常执行后续操作。请问是否需要给Asciidoctor命令添加额外参数?
批处理脚本代码
@echo off setlocal set "SolutionDir=%1" echo POST-BUILD set "filePath=%SolutionDir%SlitapScheduler\bin\Release\SlitapScheduler.exe" echo %filePath% set "filePath=%filePath:\=\\%" for /f "usebackq delims=" %%a in (`wmic DATAFILE WHERE "name='%filePath%'" get Version /format:Textvaluelist`) do ( for /f "delims=" %%# in ("%%a") do set "%%#" ) echo %Version% asciidoctor -V REM below this line commands are not executed echo :::HTML generation::: asciidoctor -v -t -a release-version=%Version% "%SolutionDir%Docs\SlitapScheduler.adoc" echo :::PDF generation::: asciidoctor-pdf -v -t -a release-version=%Version% "%SolutionDir%Docs\SlitapScheduler.adoc" echo :::SETUP generation::: iscc /O+ "%SolutionDir%Setup\neu_inno_script.iss" endlocal
脚本执行输出
C:\project\SlitapScheduler\Setup>a C:\project\SlitapScheduler\ POST-BUILD C:\project\SlitapScheduler\SlitapScheduler\bin\Release\SlitapScheduler.exe 1.0.0.16 Asciidoctor 2.0.20 [https://asciidoctor.org] Runtime Environment (ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]) (lc:IBM437 fs:UTF-8 in:UTF-8 ex:UTF-8)
解决方案
这是因为Windows下的Asciidoctor是通过批处理脚本(asciidoctor.bat)运行的,直接调用它会替换当前批处理的执行上下文,导致原脚本直接终止。解决方法很简单,在所有Asciidoctor相关命令前加上call命令,这样执行完Asciidoctor的脚本后,会回到原脚本继续执行后续命令。
修改后的完整脚本如下:
@echo off setlocal set "SolutionDir=%1" echo POST-BUILD set "filePath=%SolutionDir%SlitapScheduler\bin\Release\SlitapScheduler.exe" echo %filePath% set "filePath=%filePath:\=\\%" for /f "usebackq delims=" %%a in (`wmic DATAFILE WHERE "name='%filePath%'" get Version /format:Textvaluelist`) do ( for /f "delims=" %%# in ("%%a") do set "%%#" ) echo %Version% call asciidoctor -V echo :::HTML generation::: call asciidoctor -v -t -a release-version=%Version% "%SolutionDir%Docs\SlitapScheduler.adoc" echo :::PDF generation::: call asciidoctor-pdf -v -t -a release-version=%Version% "%SolutionDir%Docs\SlitapScheduler.adoc" echo :::SETUP generation::: iscc /O+ "%SolutionDir%Setup\neu_inno_script.iss" endlocal
补充说明
iscc不需要加call是因为它是一个独立的可执行程序(.exe),直接调用不会替换当前批处理的执行上下文,所以执行后能继续运行后续命令。call命令的作用是启动一个新的批处理上下文来执行目标脚本,执行完成后返回原上下文继续执行。
内容的提问来源于stack exchange,提问作者PiotrBzdrega
相关产品推荐
相关产品推荐

