0

In my bash script ,a private repo is being cloned which prompts for username and password. The issue is even if incorrect username or password is entered and the git authentication fails - remote: Invalid username or password and the script proceeds further ignoring it .i want the git username and password read prompts to run in loop until git authentication succeeds . In other words , if incorrect username or password is entered it should be detected by bash and the read prompts should rerun in loop until git authentication is successful

#!/usr/bin/env bash

# something ....

read -e -p "Input your github username : " username
read -e -p "Input your github password : " password
git clone https://"$username":"$password"@github.com/"$username"/repo

# something ...

How do i solve this problem ?

3
  • 1
    Are you certain about that behavior? When I try to clone a private repository using an invalid username/password, git says remote: Invalid username or password., does not create any directory, and exits with an error status. Commented Feb 10, 2021 at 19:09
  • i am sorry for that mistake , i checked properly again , that blank folder was getting created through the bash script itself and not due to git authentication failure .updated question accordingly Commented Feb 10, 2021 at 19:13
  • Password-based authentication is deprecated. Why aren't you using public key authentication instead, so that your script doesn't have to deal with authentication at all? Commented Feb 10, 2021 at 19:38

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually the script is quite long ,and i dont really want to exit at the very end if incorrect username or password is entered . is it possible to rerun the username and password read prompts and attempt to clone to repo until git authentication is successful ?
@Sachin Sure, use a loop
@Sachin Done, I have added an example with a loop. If your script becomes too complicated to maintain (for you or your team), consider switching to a different scripting language, e.g. python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.