Justin Justin -4 years ago 141
Bash Question

How to detect if a git clone failed in a bash script

How can I tell if a

git clone
had an error in a bash script?

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

If there was an error, I want to simply
exit 1

Answer Source

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
    echo >&2 message
    exit 1

if ! test "$ret" -eq 0
    echo >&2 "command failed with exit status $ret"
    exit 1

failingcommand || exit "$?"

failingcommand || { echo >&2 "failed with $?"; exit 1; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download