在我的bash脚本中,一个私有存储库正在被克隆,它会提示输入用户名和密码。问题是,即使输入了错误的用户名或密码,git身份验证失败- remote: Invalid username or password
,脚本继续忽略它,并希望git用户名和密码读取提示循环运行,直到git身份验证成功。换句话说,如果输入了错误的用户名或密码,bash应该检测到它,并且读取提示应该循环地重新运行,直到git身份验证成功为止。
#!/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 ...
我该如何解决这个问题?
发布于 2021-02-11 03:11:26
Git通常不会创建认证失败的目录。检查文件夹是否存在,如果不存在则退出:
test -d ./repo || { echo 'Dir does not exist'; exit 1; };
但在此之前,git
进程应该已经以非零退出代码退出,因此您可以这样做:
git clone "https://…@…/path/to/repo" || exit 1;
如果您想一直重试,直到命令成功,请使用循环:
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
您可能还会对How to get a password from a shell script without echoing感兴趣,因为它可以在输入密码时防止肩部冲浪。
https://stackoverflow.com/questions/66143414
复制相似问题