Git从Shell脚本中提取

Git从Shell脚本中提取,git,bash,shell,Git,Bash,Shell,我试图从bash脚本更新git repo,但遇到了一些麻烦。如果git pull之后没有其他行或字符,但如果我说have,我可以运行git pull,让它工作 #!/bin/bash git pull echo "test" 然后我得到 ' is not a git command. See 'git --help'. Did you mean this? pull 在终端中,您必须首先像Etan提到的那样执行dos2unix,而不是实际执行git pull。如果执行相反的

我试图从bash脚本更新git repo,但遇到了一些麻烦。如果git pull之后没有其他行或字符,但如果我说have,我可以运行git pull,让它工作

#!/bin/bash
git pull
echo "test"
然后我得到

' is not a git command. See 'git --help'.

Did you mean this?
        pull

在终端中,您必须首先像Etan提到的那样执行dos2unix,而不是实际执行
git pull

。如果执行相反的操作(即unix2dos),则可以重新生成相同的错误

$cat 1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
$/1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
如上所述,gitpull命令工作正常,但有一条有效的错误消息,在pull之后并没有像我第一次做的那个样提到repo。这意味着git是存在的(如果您选择了哪个git,您将看到有效的git可执行文件位置)

$unix2dos.exe./1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
现在,我的文件是DOS格式的(我在Linux机器上运行脚本)。你在用Cygwin吗

$/1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
好的,使用dos2unix将其转换回Unix行结束字符

$dos2unix.exe./1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
$/1.sh

#!/bin/bash

git pull
echo ----
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
unix2dos: converting file ./1.sh to DOS format ...
./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----
dos2unix: converting file ./1.sh to Unix format ...
fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----
它按预期工作


或者尝试使用bash-x运行该文件是否有DOS换行符?在该脚本上运行
dos2unix
是否有帮助?如果您有基于UNIX的系统,请使用apt get install git,如果您有基于Windows的系统,请手动安装git。正如@EtanReisner所建议的,您可以使实际运行的
git pull\r
@Ioan他们显然安装了git,因为他们从git收到了错误消息。