检查来自shell脚本的git push的响应

时间:2016-10-21 12:44:52

标签: git bash shell github

我试图从bash中检测到我的git push是否成功。我正在检查以前在我的脚本中的本地更改,如下所示:

if [ -z "$(git status --porcelain)" ]; then

并且工作正常。这是我尝试过的表达,但这个表达不起作用,事实上它是错误的:

if [ "$(git push --porcelain)" -eq "Done" ]; then

的产率:

Done: integer expression expected

当我从命令行运行git push --porcelain时,输出为Done。这是否意味着我应该在我的条件下检查该文本?

如果我进行之前的比较,那也不起作用,我会得到同样的错误:

1 file changed, 309 insertions(+)
Current branch master is up to date.
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.59 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
bash: [: To https://github.com/blah...
        refs/heads/master:refs/heads/master     88aff43..cf0c97c
Done: integer expression expected

2 个答案:

答案 0 :(得分:10)

git push在成功时返回零退出代码,在失败时返回非零。

所以你可以写:

if git push
then
  echo "git push succeeded"
else
  echo "git push failed"
fi

答案 1 :(得分:5)

您的状况检查应该是这样的: -

#!/bin/bash

if [[ "$(git push --porcelain)" == *"Done"* ]]
then
  echo "git push was successful!"
fi

-eq用于整数比较,==用于字符串比较。以下说明了不同之处。

[[ 00 -eq 0 ]] && echo "zero is zero, regardless of its representation"
[[ 00 = 0 ]] || echo "00 and 0 are two different strings"