-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