我正在嘗試撰寫一個批處理腳本來檢查 git repo 中的本地模塊。我只需要一個是/否輸出,但我想排除未跟蹤的檔案。
這是我在包含未跟蹤檔案時在命令列中看到的內容:
C:\local_workspace>git status --porcelain
M EWARM/stm32u575xx_flash.icf
M EWARM/stm32u575xx_sram.icf
?? EWARM/bui.h
?? EWARM/gen_build_info_header_git.bat
?? EWARM/test.h
?? EWARM/test2.h
我可以使用for命令來處理它:
C:\local_workspace>for /F "delims=" %i in ('git status --porcelain') do (echo [%i])
C:\local_workspace>(echo [ M EWARM/stm32u575xx_flash.icf] )
[ M EWARM/stm32u575xx_flash.icf]
C:\local_workspace>(echo [ M EWARM/stm32u575xx_sram.icf] )
[ M EWARM/stm32u575xx_sram.icf]
C:\local_workspace>(echo [?? EWARM/bui.h] )
[?? EWARM/bui.h]
C:\local_workspace>(echo [?? EWARM/gen_build_info_header_git.bat] )
[?? EWARM/gen_build_info_header_git.bat]
C:\local_workspace>(echo [?? EWARM/test.h] )
[?? EWARM/test.h]
C:\local_workspace>(echo [?? EWARM/test2.h] )
[?? EWARM/test2.h]
如果我排除跟蹤的檔案,我會看到以下內容:
C:\local_workspace>git status --porcelain --untracked-files=no
M EWARM/stm32u575xx_flash.icf
M EWARM/stm32u575xx_sram.icf
現在讓我們嘗試:
C:\local_workspace>for /F "delims=" %i in ('git status --porcelain --untracked-files=no') do (echo [%i])
C:\local_workspace>
沒有輸出!為什么不?
uj5u.com熱心網友回復:
for
回圈被=
視為空格,因此出于所有意圖和目的,您的代碼正在嘗試處理git status --porcelain --untracked-files no
無效的命令。
為了保留=
,它需要用 a 轉義^
:git status --porcelain --untracked-files^=no
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507904.html
標籤:批处理文件