[英]Escape spaces in batch file that calls msysgit sh.exe?
我有一個批處理文件 run-sh.bat
,它試圖將命令傳遞給sh.exe
的sh.exe :
rem My Batch File
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "%1" "
第一個參數作為命令傳遞。 一個簡單的工作示例:
run-sh.bat "' echo Hello '"
...
Hello
但是,如果命令的路徑帶有空格,則該路徑會在第一個空格處被切掉:
run-sh.bat "' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"
...
C:\Windows\Path\That
現在,如果我將整個命令直接粘貼到批處理文件中 (而不是使用參數):
rem My Batch File
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "%1" "
cmd /c " "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c ""' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"" "
然后您可以看到第二回聲的路徑正確了:
run-sh.bat "' echo "C:\Windows\Path\That Contains\Some Spaces\In\It" '"
...
C:\Windows\Path\That
C:\Windows\Path\That Contains\Some Spaces\In\It
如何獲得它作為參數的作用?
我應該修改批處理文件嗎? 還是更改我在命令行格式化參數的方式?
經過測試的解決方案:
Using CMD:
rem My Batch File
CMD /c ""%programfiles(x86)%\Git\bin\sh.exe" --login -i -c '%*'"
You don´t have to use CMD /C though. You can run it directly to avoid extra quotes:
rem My Batch File
"%programfiles(x86)%\Git\bin\sh.exe" --login -i -c '%*'
然后,當您調用它時,僅在路徑上使用引號:
run-sh.bat echo "C:\Windows\Path\That Contains\Some Spaces\In\It"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.