如何在bash脚本中检测git clone是否失败

时间:2012-12-10 01:36:20

标签: linux git bash shell

如何判断git clone是否在bash脚本中出错?

git clone git@github.com:my-username/my-repo.git

如果出现错误,我只想exit 1;

3 个答案:

答案 0 :(得分:22)

以下是一些常见表格。哪个是最好的选择取决于你做什么。您可以在单个脚本中使用它们的任何子集或组合,而不会是错误的样式。


if ! failingcommand
then
    echo >&2 message
    exit 1
fi

failingcommand
ret=$?
if ! test "$ret" -eq 0
then
    echo >&2 "command failed with exit status $ret"
    exit 1
fi

failingcommand || exit "$?"

failingcommand || { echo >&2 "failed with $?"; exit 1; }

答案 1 :(得分:5)

您可以执行以下操作:

git clone git@github.com:my-username/my-repo.git || exit 1

或者执行:

exec git clone git@github.com:my-username/my-repo.git

后者将允许克隆操作接管shell进程,如果失败,则返回错误。您可以找到有关exec here的更多信息。

答案 2 :(得分:1)

方法1:

main(args: Array[String])

方法2:

git clone git@github.com:my-username/my-repo.git || exit 1