0

hi guys i have problem in bashscript aws like this:

#!/bin/bash
for x in $(aws resourcegroupstaggingapi get-resources --resource-type-filters eks --query "ResourceTagMappingList[*].{ResourceARN:ResourceARN}" --output text)
do 
for y in $(aws resourcegroupstaggingapi get-resources --resource-type-filters eks --query "ResourceTagMappingList[*].{Project:Tags[?Key=='Name']|[0].Value,Project:Tags[?Key=='Project']|[0].Value}" --output text)
do
echo "$x"" ""$y"

done
done

the output for each aws code if not use FOR is :

value of x if not use for:

$aws resourcegroupstaggingapi get-resources --resource-type-filters eks --query "ResourceTagMappingList[*].{ResourceARN:ResourceARN}" --output text

arn:aws:eks:ap-southeast-1:id:cluster/fetcher    
arn:aws:eks:ap-southeast-1:id:cluster/Hotel

value Y if not use for:

$aws resourcegroupstaggingapi get-resources --resource-type-filters eks --query "ResourceTagMappingList[*].{Project:Tags[?Key=='Name']|[0].Value,Project:Tags[?Key=='Project']|[0].Value}" --output text

Extra API
Hotel Booking

but the output from the shell script is:

arn:aws:eks:ap-southeast-1:id:cluster/fetcher Extra
arn:aws:eks:ap-southeast-1:id:cluster/fetcher API
arn:aws:eks:ap-southeast-1:id:cluster/fetcher Hotel
arn:aws:eks:ap-southeast-1:id:cluster/fetcher Booking
arn:aws:eks:ap-southeast-1:id:cluster/Hotel Extra
arn:aws:eks:ap-southeast-1:id:cluster/Hotel API
arn:aws:eks:ap-southeast-1:id:cluster/Hotel Hotel
arn:aws:eks:ap-southeast-1:id:cluster/Hotel Booking

can some one fix this code with the right output like this with spaceline in value y not in new line example:

arn:aws:eks:ap-southeast-1:id:cluster/fetcher Extra API
arn:aws:eks:ap-southeast-1:id:cluster/Hotel Hotel Booking
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    See [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor), and [BashFAQ #1](https://mywiki.wooledge.org/BashFAQ/001), which describes the proper way to iterate over line-oriented input in bash. – Charles Duffy Mar 26 '22 at 13:12
  • 1
    Beyond that, if you want _two_ lines of output, not four, you shouldn't be using two nested for loops at all. When you put a `for` loop inside another, you run the inner loop every time the outer loop happens, so you get (number-of-inside-items * number-of-outside-items) output elements. That's not just a bash thing, that's true in every language where `for` has typical semantics. – Charles Duffy Mar 26 '22 at 13:15
  • 1
    [How can I iterate over multiple lists in a shell script?](https://stackoverflow.com/questions/45011069/how-can-i-iterate-over-multiple-lists-in-a-shell-script) is relevant to that. `paste <(first-aws-command) <(second-aws-command)` is maybe even easier (note that it requires bash, and won't work with `sh`). – Charles Duffy Mar 26 '22 at 13:18

0 Answers0