如何判断git clone
是否在bash脚本中出错?
git clone git@github.com:my-username/my-repo.git
如果出现错误,我只想exit 1
;
答案 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