[英]base64 encode and decode of aws command to retrieve some fields
Could someone explain the below code please?有人可以解释下面的代码吗?
The below code is producing the expected results with username.下面的代码使用用户名产生了预期的结果。
for row in $(echo "${es_eh}" | jq -r '.Events[] | @base64'); do
echo "${row}" | base64 --decode | jq -r '.Username'
I didn't understand the purpose of doing base64 encode and then doing decode of the same string inside loop to retrieve username.我不明白进行 base64 编码然后在循环内对相同字符串进行解码以检索用户名的目的。
This is not working when I remove base64 encode and decode.当我删除 base64 编码和解码时,这不起作用。
for row in $(echo "${es_eh}" | jq -r '.Events[]'); do
echo "${row}"| jq -r '.Username'
es_eh contains the json output of the below aws command, es_eh 包含以下 aws 命令的 json output,
es_eh="$(aws cloudtrail --region us-east-1 lookup-events --lookup-attributes AttributeKey=EventSource,AttributeValue=route53.amazonaws.com --max-items 50 --start-time "${start_date}" --end-time "${end_date}" --output json)"
Without the encoding, the output of the first jq
is more than one row.没有编码,第一个
jq
的output不止一行。 The loop iterates over the lines and fails, as none of them contains a valid JSON. With the | @base64
循环遍历这些行并失败,因为它们都不包含有效的 JSON
| @base64
| @base64
, each subobject is encoded to a single row, inflated back to a full JSON object by base64 --decode
. | @base64
,每个子对象都被编码为一行,通过 base64 --decode 膨胀回完整的 JSON base64 --decode
。
To see the rows, try outputting $row before processing it.要查看行,请在处理之前尝试输出 $row。
When you use $( )
without quotes around it, the result gets split into "words", but the shell's definition of a "word" is almost never what you want (and certainly has nothing to do with the json entries you want it split into).当你使用不带引号的
$( )
时,结果会被拆分成“单词”,但 shell 对“单词”的定义几乎从来不是你想要的(当然与你想要它拆分的 json 条目无关进入)。 This sort of thing is why you should almost never use unquoted expansions.这种事情就是为什么你几乎不应该使用不带引号的扩展。
Converting the output entries to base64 makes them wordlike enough that shell word splitting actually does the right thing.将 output 条目转换为 base64 使它们足够像单词,以至于 shell 单词拆分实际上做了正确的事情。 But note: some base64 encoders split their output into lines, which would make each line be treated as a separate "word".
但请注意:一些 base64 编码器将其 output 分成几行,这将使每一行都被视为一个单独的“单词”。 If
jq
's base64 encoding did this, this code would fail catastrophically on large events.如果
jq
的 base64 编码执行此操作,则此代码将在大型事件上灾难性地失败。
Transforming the for
loop into a while
loop should fix the problem:将
for
循环转换为while
循环应该可以解决问题:
while read -r row; do
echo "${row}" | jq -r '.Username'
done < <(echo "${es_eh}" | jq -c -r '.Events[]')
Note that in the outer jq
, I used option -c
to put output in a single ine.请注意,在外部
jq
中,我使用选项-c
将 output 放入单个 ine 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.