How to detect if a git clone failed in a bash script
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; }
Test to determine if git clone command succeeded
The return value is stored in $?. 0 indicates success, others indicates error.
some_command
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
I haven't tried it with git, but I hope this works.
how to detect unsuccessful git authentication in bash?
Git will usually not create the directory of authentication failed. Check if the folder exists and exit if it does not:
test -d ./repo || { echo 'Dir does not exist'; exit 1; };
But before that, the git
process should already exit with a non-zero exit code, so you could do this instead:
git clone "https://…@…/path/to/repo" || exit 1;
If you want to keep retrying until the command succeeds, use a loop:
read -e -p 'Input your github username: ' username
read -e -p 'Input your github password: ' password
while ! git clone "https://…@…/path/to/repo"; do
echo 'Error cloning, please retry' >&2;
read -e -p 'Input your github username: ' username
read -e -p 'Input your github password: ' password
done
You might also be interested in How to get a password from a shell script without echoing for preventing shoulder surfing while entering the password.
How to check if GIT has fully cloned a repository?
Have a look at it.
I think You are expecting this code.
https://stackoverflow.com/a/13715406/2959196
Also Have a look at the conversation. It might help you better.
How to detect if a git clone failed in a bash script
Shell: don't fail git clone if folder already exists
The most stable solution would be to simply let it fail and print the error message. If you think that's too ugly for your scenario, you may redirect it to /dev/null:
folder="foo"
if ! git clone "${url}" "${folder}" 2>/dev/null && [ -d "${folder}" ] ; then
echo "Clone failed because the folder ${folder} exists"
fi
Otherwise you can do something like this:
if [ ! -d "$FOLDER" ] ; then
git clone "$URL" "$FOLDER"
fi
but that would be vulnerable to race conditions.
How can I check in a Bash script if my local Git repository has changes?
What you're doing will almost work: you should quote $CHANGED
in case it's empty, and -z
tests for empty, which means no changes. What you meant was:
if [ -n "$CHANGED" ]; then
VN="$VN-mod"
fi
A quote from Git's GIT-VERSION-GEN
:
git update-index -q --refresh
test -z "$(git diff-index --name-only HEAD --)" ||
VN="$VN-dirty"
It looks like you were copying that, but you just forgot that detail of quoting.
Of course, you could also just do this:
if git diff-index --quiet HEAD --; then
# No changes
else
# Changes
fi
Or if you only care about the "something has changed" case:
if ! git diff-index --quiet HEAD --; then
VN="$VN-mod"
fi
Using --quiet
has the benefit that Git can stop processing as soon as it encounters a single diff, so it may not have to check your entire work tree.
Aborting a shell script if any command returns a non-zero value
Add this to the beginning of the script:
set -e
This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if
, while
, or until
test, or part of an &&
or ||
list.
See the bash manual on the "set" internal command for more details.
It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script. I personally start almost all portable shell scripts with set -e
.
If I'm working with bash specifically, I'll start with
set -Eeuo pipefail
This covers more error handling in a similar fashion. I consider these as sane defaults for new bash programs. Refer to the bash manual for more information on what these options do.
Related Topics
Delete a Column from a Delimited File in Linux
How to Automate Telnet Session Using Expect
Where Does Pp (Par) Unpack Add (-A) Files
How to Get Cmake to Use the Default Compiler on System Path
Pyqt5 Error "Pycapsule_Getpointer Called with Incorrect Name"
Concurrency of Posix Threads in Multiprocessor MAChine
Mechanism of Clipboard of Xwindow
How to Make One Linux Kernel Module Depend on Another External Module with Depmod
Qimage to Cv::Mat Convertion Strange Behaviour
Why Does '/Proc/Meminfo' Show 32Gb When Aws Instance Has Only 16Gb
Bash Output Stream Write to a File
Get Filesystem Mount Point in Kernel Module
How to Append Contents of Multiple Files into One File
Redirecting Tcp-Traffic to a Unix Domain Socket Under Linux