简体   繁体   English

在 shell 中使用“eval”时不考虑 SSH 密钥

[英]SSH keys not considered while using "eval" in shell

I am trying to do a git clone in a shell script where the repo URL is evaluated dynamically.我正在尝试在动态评估 repo URL 的 shell 脚本中执行 git clone。 I have tried below in my script:我在我的脚本中尝试过:

git clone 'git_repo_url' works fine git clone 'git_repo_url'工作正常

x='git_repo_url'; git clone $x x='git_repo_url'; git clone $x works fine x='git_repo_url'; git clone $x工作正常

I am using eval to dynamically create the repo URL, and echo $(eval xyz) gives the exact same repo URL as above ie 'git_repo_url'我正在使用 eval 动态创建 repo URL,并且echo $(eval xyz)给出了与上面完全相同的 repo URL,即'git_repo_url'

  • However git clone $(eval xyz) fails with Permission denied (publickey) error.但是git clone $(eval xyz)失败并出现Permission denied (publickey)错误。

  • Also, x=$(eval xyz) and git clone $x also fails Permission denied (publickey) error.此外, x=$(eval xyz)git clone $x也会失败Permission denied (publickey)错误。

My SSH keys are configured in the default location ~/.ssh and also have ~/.ssh/config in place.我的 SSH 密钥配置在默认位置 ~/.ssh 并且也有 ~/.ssh/config。 I am unable to figure out what is going wrong here.我无法弄清楚这里出了什么问题。

This is what I see with (set -x; git clone $(eval xyz))这就是我看到的(set -x; git clone $(eval xyz))

git clone ''\''git_repo_url'\'''

So those quotes and slashes are causing the issue, but not sure why.所以这些引号和斜杠导致了这个问题,但不知道为什么。

Why I am using eval为什么我使用 eval

  1. The "git_repo_url" for various projects are exported as environment variables.各种项目的“git_repo_url”被导出为环境变量。 The format of the environment variable is as follows:环境变量的格式如下:

    project_<project_name>_git='git_repo_url'

  2. <project_name> is passed as the first input parameter to the shell script that does the git clone <project_name>作为第一个输入参数传递给执行 git clone 的 shell 脚本

  3. So to form the environment variable, I am using eval:所以为了形成环境变量,我使用了eval:

    $(eval echo \$\{project_$1_git\})

Rather than use eval to construct an echo of the value you want using the arbitrary-command-execution hammer, use bash's expansion-indirection option, it's simple, safe, efficient, ...:与其使用 eval 使用任意命令执行锤子来构造您想要的值的回显,不如使用 bash 的扩展间接选项,它简单、安全、高效,......:

urlname=project_${project_name}_git
git clone ${!urlname}

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

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