最近做了一个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"
具体原因,查看官方文档:
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).