简体   繁体   English

git 克隆的 Shell 脚本不能将变量用作密码

[英]Shell script for git clone doesn't work with a variable as password

I'm getting trouble with a shell script for git, this script will ask the password for a robotic user account to make a clone/push like this:我在使用 git 的 shell 脚本时遇到问题,该脚本将询问机器人用户帐户的密码以进行如下克隆/推送:

echo 'write the pass'
read pass
git clone https:\\user:$pass@bitbucket... destination

The problem is that it shows me: fatal: Authentication failed for 'https:\\user:$pass@bitbucket...'问题是它向我显示: fatal: Authentication failed for 'https:\\user:$pass@bitbucket...'

I tried with https:\\user:${pass}@bitbucket... and same result我尝试使用https:\\user:${pass}@bitbucket...结果相同

If I try directly without any variable like git clone https:\\user:password@bitbucket... destination it works perfect.如果我直接尝试不使用任何变量,例如git clone https:\\user:password@bitbucket... destination它完美。

Do you guy know what's the problem?大佬知道是什么问题吗?

You have a couple issues here.你在这里有几个问题。 One is that you're using backslashes instead of forward slashes.一种是您使用反斜杠而不是正斜杠。 That's likely to be interpreted poorly by the shell. shell 可能对这理解很差。

Another issue is the quoting problem: you may have a space in the password, and if so, the shell will interpret it as two arguments.另一个问题是引用问题:密码中可能有空格,如果有,shell 会将其解释为两个 arguments。

Finally, if the password you're using contains certain characters, you'll need to escape them for the password to work correctly.最后,如果您使用的密码包含某些字符,您需要对它们进行转义以使密码正常工作。 For example, some passwords contain an equal sign, which must be encoded as %3D .例如,某些密码包含等号,必须将其编码为%3D There isn't a good way to do this in shell, but if you have Perl installed (which you likely do with Git), you can change this to the following:在 shell 中没有这样做的好方法,但是如果您安装了 Perl(您可能使用 Git),您可以将其更改为以下内容:

echo 'write the pass'
read pass
pass="$(echo "$pass" | perl -pe 's/([^A-Za-z0-9._~-])/sprintf "%%%02X", ord($1)/ge')"
git clone https://user:$pass@bitbucket.org/...

Since a percent-encoded password will never contain a space, it's no longer necessary to quote the URL.由于百分比编码的密码永远不会包含空格,因此不再需要引用 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM