-1
我正在寫一個簡單的腳本來檢查我的git分支是否是最新的,但是當我嘗試比較git status與我的預定義字符串如果統計信息從不觸發,即使echo %% i,並顯示我正在比較的完全相同的字符串。比較兩個看似相同的變量不觸發if語句BATCH文件
SETLOCAL ENABLEDELAYEDEXPANSION
echo off
D:
cd D:\GitHub\goworkspace\src\Programvareutvikling
set cmd="git status -uno"
set msg=Your branch is up-to-date with 'origin/Server'.
FOR /F "tokens=*" %%i IN (' %cmd% ') DO (
REM this line never triger even when it seems like it should
set X=%%i
IF !X! == %msg% Echo uptodate
)
pause
任何幫助將被認爲是,這是我在堆棧交換的第一個問題,所以請告訴我,如果我做錯了什麼。備用的靈活性也很好,但我認爲不必安裝額外的程序。
This is the output:
echo %msg% >Your branch is up-to-date with 'origin/Server'.
echo !X! >Your branch is up-to-date with 'origin/Server'.
echo %msg% >Your branch is up-to-date with 'origin/Server'.
echo !X! >nothing to commit (use -u to show untracked files)
Press any key to continue . . .
解決方案: 我沒有在變量周圍使用引號。
IF /I "%%~i"=="%msg%" Echo uptodate
我會從這行刪除引號:'set cmd =「git status -uno」' – Squashman
這樣做和程序是否像以前一樣運行,輸出仍然完全相同,但if語句仍然會不觸發 – coder5432
因爲字符串中有空格,所以您需要在比較中加上引號,我只是使用FOR變量而不是。 'IF/I「%%〜i」==「%msg%」Echo uptodate「。 – Squashman