【问题标题】:Shell script for git clone doesn't work with a variable as passwordgit clone 的 shell 脚本不能使用变量作为密码
【发布时间】:2019-10-24 21:59:25
【问题描述】:

我在使用 git 的 shell 脚本时遇到问题,该脚本会询问机器人用户帐户的密码,以便像这样进行克隆/推送:

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

问题是它向我展示了: fatal: Authentication failed for 'https:\\user:$pass@bitbucket...'

我尝试了https:\\user:${pass}@bitbucket...,结果相同

如果我直接尝试没有任何变量,例如 git clone https:\\user:password@bitbucket... destination 效果很好。

你知道是什么问题吗?

【问题讨论】:

  • 调试这个在克隆命令之前添加echo "$pass"也总是双引号你的变量。

标签: git shell


【解决方案1】:

这里有几个问题。一种是您使用反斜杠而不是正斜杠。这可能会被 shell 解释得很差。

另一个问题是引用问题:密码中可能有空格,如果有,shell 会将其解释为两个参数。

最后,如果您使用的密码包含某些字符,您需要对它们进行转义才能使密码正常工作。例如,某些密码包含等号,必须将其编码为%3D。在 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/...

由于百分比编码的密码永远不会包含空格,因此不再需要引用 URL。

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 2017-11-05
    • 1970-01-01
    • 2016-03-10
    • 2013-05-10
    • 2020-01-20
    • 2018-06-22
    • 2013-08-17
    • 2011-06-04
    相关资源
    最近更新 更多