简体   繁体   English

Bash 从脚本导出 credstash 值

[英]Bash export credstash values from script

So i'm trying to implement a build script that pulls down our credstash keys from DynamoDB and then sets them in the environment running the script, I need to the commands afterward to have access to those environment variables to compile some YML.因此,我正在尝试实现一个构建脚本,该脚本从 DynamoDB 中提取我们的 credstash 密钥,然后将它们设置在运行该脚本的环境中,之后我需要使用这些命令来访问这些环境变量以编译一些 YML。

Here's what I have(kinda working):这是我所拥有的(有点工作):

    #!/bin/bash

    creds=$(credstash getall)
    declare -a arrayKeys=($(echo $creds | ./jq '[to_entries[] | .key]' | tr ',' '\n'))

    for ((i=1; i<(${#arrayKeys[*]} -1); i++));
    do
       key=$( printf '%s:' "${arrayKeys[i]}" )
       key=${key%:}

      export key="foo"
    done

This actually seems to run through but when I do printenv after I don't see the keys that I'm after, note I'm setting them to the value foo just to get the iterator stuff working, when this is finished it'll use JQ to pull the value from the JSON I fetched earlier.这实际上似乎运行了但是当我在没有看到我所追求的键之后执行 printenv 时,请注意我将它们设置为值 foo 只是为了让迭代器工作,当这完成后它会使用 JQ 从我之前获取的 JSON 中提取值。

Sample JSON:示例 JSON:

{
"db.password" : "Some password",
"db.username" : "Some username"
}

Note, as you might be able to tell, I'm no bash scripting expert so this what I've cobbled together after doing a bit of reading.请注意,正如您可能会说的,我不是 bash 脚本专家,所以这是我在阅读了一些书后拼凑出来的。

EDIT编辑

So after the comments below I've now got:所以在下面的评论之后,我现在得到了:

#!/bin/bash

creds=$(credstash getall)
declare -a arrayKeys=($(echo $creds | ./jq --raw-output '[to_entries[] | .key]' | tr ',' '\n'))

for ((i=1; i<(${#arrayKeys[*]} -1); i++));
do
   key=$( printf '%s:' "${arrayKeys[i]}" )
   key=${key%:}
   export eval $key='foo'
done

Which yields:其中产生:

bash: export: `"db.username"=foo': not a valid identifier

bash + jq solution: bash + jq解决方案:

while read -r key val; do 
    declare -x "$key"="$val"
done < <(jq -r 'to_entries[] | [(.key | gsub("\\.";"_")), .value] | @tsv' <<<"$creds")

Check declared variable:检查声明的变量:

$ echo "$db_username"
Some username

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

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