这最终成为路径问题。我在脚本的其他地方使用“路径”作为我的变量之一,但没有意识到这实际上编辑了环境变量 PATH。
原始问题:
我正在写一个批处理文件。在批处理文件中,我有以下内容:
FOR /F "tokens=*" %%a IN ('git branch -r') DO CALL :SOMELABEL %%a
EXIT
:SOMELABEL
git status
START /B /WAIT CMD /C git status
FOR /F "tokens=*" %%n IN ('git status') DO ECHO %%n
GOTO :EOF
'git status' 只是一个例子。 3 个 'git status' 命令中的每一个都返回“'git' 未被识别为内部或外部命令、可运行的程序或批处理文件。”
通常,这将是一个环境变量问题 (PATH),但它的独特之处在于第一个 git 命令有效(“git branch -r”)。我可能还有 5 个其他批处理文件也可以成功使用 git 命令。关于为什么后面的 git 命令在上面的代码中不起作用有什么想法吗?
如果是由于权限问题(比如在执行一个 git 命令时无法执行另一个 git 命令之类的事情),知道我如何使这个脚本工作(最好不必创建临时文件)吗?还是为第一个命令的输出制作一个临时文件是唯一的方法?
最佳答案
不过,这应该是一个 PATH
问题。
OP Lectrode确认:
It was a path issue.
I just realized that in my original script I was usingpath
for one of my variables.
I did not realize that was setting the environmentPATH
variable.Apparently git uses that "batch proxy" thing.
The command works when you changegit status
toCALL git status
.
原答案:
我刚刚成功测试了以下脚本,遵循“DOS Batch - Function Tutorial ”中描述的语法(并使用我安装的 'which' 命令 through gow ):
@echo off
echo.PATH=%PATH%
which git.exe
FOR /F "tokens=*" %%a IN ('git branch -r') DO CALL :myDosFunc %%a
call:myDosFunc
echo.&pause&goto:eof
:myDosFunc - here starts my function identified by it`s label
echo. with param %~1
which git.exe
git status
goto:eof
当在 Windows 上的 Git 存储库中执行时,它确实返回了:
C:\prog\git\tests\my_repo>..\c.bat
PATH=Z:\apps\git176\bin;...
Z:\apps\git176\bin\git.exe
with param origin/master
Z:\apps\git176\bin\git.exe
# On branch master
nothing to commit (working directory clean)
with param
Z:\apps\git176\bin\git.exe
# On branch master
nothing to commit (working directory clean)
所以它似乎有效。 (作为 pointed out by Magnus ,我不使用 exit
)
关于git - "' git ' is not recognized as an internal"blah blah 即使命令第一次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815223/