如何检测 git clone 在 bash 脚本中是否失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13793836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect if a git clone failed in a bash script
提问by Justin
How can I tell if a git clonehad an error in a bash script?
如何判断git clonebash 脚本中是否有错误?
git clone [email protected]:my-username/my-repo.git
If there was an error, I want to simply exit 1;
如果有错误,我只想简单地exit 1;
回答by Jo So
Here are some common forms. Which is the best to choose depends on what you do. You can use any subset or combination of them in a single script without it being bad style.
以下是一些常见的形式。选择哪个最好取决于你做什么。您可以在单个脚本中使用它们的任何子集或组合,而不是糟糕的风格。
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; }
回答by John Szakmeister
You could do something like:
你可以这样做:
git clone [email protected]:my-username/my-repo.git || exit 1
Or exec it:
或者执行它:
exec git clone [email protected]:my-username/my-repo.git
The latter will allow the shell process to be taken over by the clone operation, and if it fails, return an error. You can find out more about exec here.
后者将允许 shell 进程被克隆操作接管,如果失败,则返回错误。您可以在此处找到有关 exec 的更多信息。
回答by Kalpesh Panchal
Method 1:
方法一:
git clone [email protected]:my-username/my-repo.git || exit 1
Method 2:
方法二:
if ! (git clone [email protected]:my-username/my-repo.git) then
exit 1
# Put Failure actions here...
else
echo "Success"
# Put Success actions here...
fi

