bash 从 Shell 脚本中提取 Git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24637183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:49:34  来源:igfitidea点击:

Git Pull from Shell script

gitbashshell

提问by VangardMk

I'm trying to update a git repo from a bash script and I've run into some trouble. I can run git pulland have it work if there is no other lines or characters after the git pull but if I say have

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

#!/bin/bash
git pull
echo "test"

then I get

然后我得到

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

Did you mean this?
        pull

in the terminal instead of it actually doing the git pull.

在终端中,而不是实际执行git pull.

回答by AKS

You have to do dos2unix first like Etan mentioned. I can regenerate the same error if I do the reverse (i.e. unix2dos)

你必须先像 Etan 提到的那样做dos2unix。如果我做相反的事情,我可以重新生成相同的错误(即 unix2dos)

$ cat 1.sh

$猫1.sh

#!/bin/bash

git pull
echo ----

$ ./1.sh

$ ./1.sh

fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----

See above, git pull command worked but with a valid error message that no repo mentioned after pull as im doing first time. That means git exists (if you do which gityou'll see valid git executable location).

见上文, git pull 命令工作但有一个有效的错误消息,在我第一次做 pull 后没有提到 repo。这意味着 git 存在(如果您执行which git您将看到有效的 git 可执行文件位置)。

$ unix2dos.exe ./1.sh

$ unix2dos.exe ./1.sh

unix2dos: converting file ./1.sh to DOS format ...

Now, my file is in DOS format (and Im running the script in Linux machine). Are you using Cygwin?

现在,我的文件是 DOS 格式(我在 Linux 机器上运行脚本)。你在使用 Cygwin 吗?

$ ./1.sh

$ ./1.sh

./1.sh: line 2: $'\r': command not found
' is not a git command. See 'git --help'.

Did you mean this?
        pull
----

OK, convert it back to Unix line ending chars using dos2unix

好的,使用 dos2unix 将其转换回 Unix 行尾字符

$ dos2unix.exe ./1.sh

$ dos2unix.exe ./1.sh

dos2unix: converting file ./1.sh to Unix format ...

$ ./1.sh

$ ./1.sh

fatal: Not a git repository (or any parent up to mount parent /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
----

It works as expected.

它按预期工作。

OR try running with bash -x

或者尝试使用bash -x运行