gpt4 book ai didi

linux - 如何检测 bash 脚本中的 git clone 是否失败

转载 作者:IT王子 更新时间:2023-10-29 00:24:45 27 4
gpt4 key购买 nike

我如何判断 git clone 在 bash 脚本中是否有错误?

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

如果出现错误,我想简单地exit 1

最佳答案

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


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; }

关于linux - 如何检测 bash 脚本中的 git clone 是否失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com