重击-根据输出做出决策?

时间:2019-05-27 15:01:42

标签: bash git

我正在用BASH编写很多进程的脚本。如果某些项目在Git中已经是最新的,我不想构建它们。

如果发出git pull并且结果类似:

Already on 'master'
Your branch is up-to-date with 'origin/master'.
From https://github.com/xxx/yyyy
 * branch            master     -> FETCH_HEAD
Already up-to-date.

如何根据git pull的输出做出决定?

尝试:

upstream ⚡ master) 17≡ $ git status
On branch master
nothing to commit, working tree clean

(upstream ⚡ master) 17≡ $ git pull origin master
From github.com:xxx/yyy  m
 * branch            master     -> FETCH_HEAD
Already up to date.

(upstream ⚡ master) 17≡ $ git pull origin master 2>/dev/null |grep -q "Already up-to-date"

(upstream ⚡ master) 17≡ $

尝试2:

$ git pull origin master 2>/dev/null | grep -q "Already up-to-date"; echo $?
1
$

尝试3:

$ git pull origin master 2>/dev/null |grep -q "Already up-to-date"; echo $?
1

$ git pull origin master
From github.com:cocos2d/cocos2d-x-docs
 * branch            master     -> FETCH_HEAD
Already up to date.

$ git status
On branch master
nothing to commit, working tree clean

$ git pull origin master 2>/dev/null |grep -q "Already up-to-date"; echo $?
1

尝试4:

$ git pull origin master 2>/dev/null |cat
Already up to date.

$ git pull origin master 2>/dev/null | grep -q "Already up-to-date"; echo $?
1

1 个答案:

答案 0 :(得分:1)

如果git pull的输出包含Already up-to-date,则将return true (0)

git pull | grep -q "Already up-to-date"

然后,您可以将其放入if语句中以有条件地采取措施。例如:

if git pull | grep -q "Already up-to-date"; then
    echo "Building stuff"
else
    echo "Not building stuff"
fi

您的尝试失败了,因为git打印的up to date中没有破折号,但是grep命令正在搜索带有破折号的up-to-date,这就是示例输出说。 (顺便说一句,我正在使用git 2.7.4来打印up-to-date。)这种输出可能会在git的两个版本之间发生变化,因此有一种更稳定的方法来检查这种输出,但是我我不确定这是什么:/