使用 shell 脚本进行 git pull

使用 shell 脚本进行 git pull

我有一个 shell 脚本,用于从 git 提取我的存储库。通常,它会收到凭据,提取工作正常。

我的问题是,如果凭证错误,导致身份验证失败,会发生什么情况。我该如何捕获此错误并停止 shell 脚本?

答案1

所有命令在执行完成后都会返回一个单字节值(从 0 到 255)。通常,返回值为 0 表示成功,非 0 表示出现某种问题。各种 shell 都有检查返回值是否为 0 并可对其采取行动的构造。

#!/bin/bash
if git ...
then
  echo "git succeeded"
fi

if ! git ...
then
  echo "git failed"
fi

git ... || echo "git failed"
git ... && echo "git succeeded"

相关内容