bat脚本中调用另一个bat的坑

12 篇文章 0 订阅
本文讲述了作者在Windows环境下使用Gawk工具编写脚本时,遇到调用bat文件问题,通过理解批处理命令call的作用,成功解决了在子脚本结束后返回的问题。讨论了批处理文件调用的细节和MS-DOS文档中的说明。
摘要由CSDN通过智能技术生成

最近做了一个windows下用gawk工具来校验表格的工具, 写了一个bat文件,

具体脚本如下:

@ECHO OFF
.\gawk\gawk.exe -F","  "{if(NR > 1) {filename = $1; field1 = $2; field2 = $3; cmd=\".\\\src\\\read_field.bat \"filename\" \"field1\" \"field2; print cmd;}}" .\Checklist.csv > .\src\tmp_cmd.bat
.\src\tmp_cmd.bat > output.txt
start notepad "output.txt"

脚本的最后想直接把结果文件打开,但是发现只执行到生成output.txt就没反应了,一直在linux下写脚本,这windows下真不熟,这就躺坑了,简单屏蔽代码调试发现是执行tmp_cmd.bat这个脚本引起的,于是搜了下bat中调用bat的正确姿势,发现需要加call,最终代码如下:

@ECHO OFF
.\gawk\gawk.exe -F","  "{if(NR > 1) {filename = $1; field1 = $2; field2 = $3; cmd=\".\\\src\\\read_field.bat \"filename\" \"field1\" \"field2; print cmd;}}" .\Checklist.csv > .\src\tmp_cmd.bat
call .\src\tmp_cmd.bat > output.txt
start notepad "output.txt"

具体原因,查看官方文档:

call | Microsoft DocsReference article for the call command, which calls one batch program from another without stopping the parent batch program.https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call

Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call

 Note

Call has no effect at the command prompt when it is used outside of a script or batch file.

实在是没找到官方文档,只能找到一个介绍 batch file的网站MS-DOS Batch Introduction.,里面有一句

If, within a batch file, another batch file is invoked by filename , that file is jumped to without return. But if it is invoked by CALL filename , it is called as a subroutine and, when it finishes, control returns to the next statement of the calling file (CALL is only from MS-DOS 3.3???; before, use COMMAND /C).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值