-1

I am trying to loop through an array elements called pfl which contains --profile and another rgn which contains --region values respectively.

I am not sure how i can call them correctly in a for loop.

#!/bin/bash
#
# below the --profiles defined as "pfl"
pfl="""
dev
qa
seoul
emea
apac
amec
mde
deploy
getprodkey
phx
"""
# below is `--region` defined as "rgn"

rgn="""
eu-north-1
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-3
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2
"""


for loc in "${pfl[@]}"; do aws ec2 describe-vpcs --region "${rgn[$@]}" --profile $loc --query "Vpcs[*].{ CIDR: CidrBlock, STATE: State}" --output text;done

When i am invoking the above script it gives issues like Unknown options: seoul, emea, apac, amec, mde, deploy, getprodkey, phx, qa.

However if i shall try that as a single command it will run without issues.

$ aws ec2 describe-vpcs --region us-west-2 --query "Vpcs[*].{CIDR: CidrBlock, STATE: State, VPCID: VpcId, OWNER: OwnerId, ASSOCID: CidrBlockAssociationSet.AssociationId}" --profile phx --output text

None    192.168.0.0/17    947877767552    available       vpc-0c13t7787897ea4b9
user2023
  • 452
  • 5
  • 22
  • 2
    Typos: you mean `[@]` not `[$@]`; and your variables are strings, not arrays. Tangentially the correct syntax is `"`, not `"""`, though the difference is harmless. – tripleee Oct 01 '22 at 09:42
  • Eventually `"${rgn[@]}"` would expand into the individual strings in the array, which are not valid options, either. It's not exactly clear what you were hoping to accomplish. If you can pass in multiple `--region` options, you will need to repeat it before each region name. Or if the option admits a pattern to select multiple regions, you'd have to build that string from the array somehow. – tripleee Oct 01 '22 at 09:45
  • Probably try https://shellcheck.net/ before asking for human assistance; then if you still need help, please review the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre] before proceeeding. – tripleee Oct 01 '22 at 09:48
  • @tripleee, thnx for your input .. however i have give the information as i understand , and yes i need to change `region` and `profile` for each iteration to get the value. I ahve already provided the standalone example which may be helpful for calrity. – user2023 Oct 01 '22 at 09:50
  • No, you haven't; but if you did, this would seem to be a duplicate of existing questions (canonical https://stackoverflow.com/questions/17403498/iterate-over-two-arrays-simultaneously-in-bash) – tripleee Oct 01 '22 at 10:38

1 Answers1

3

If you put your data into proper bash arrays instead of strings then you can loop through them easily:

#!/bin/bash

pfl=( dev qa seoul emea apac amec mde deploy getprodkey phx )

rgn=(
    eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1
    ap-northeast-3 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1
    ap-southeast-1 ap-southeast-2 eu-central-1
    us-east-1 us-east-2 us-west-1 us-west-2
)

for p in "${pfl[@]}"
do
    for r in "${rgn[@]}"
    do
        aws ec2 describe-vpcs \
            --region "$r" \
            --profile "$p" \
            --query 'Vpcs[*].{ CIDR: CidrBlock, STATE: State}' \
            --output text
    done
do

remark: I don't think that you can query multiple regions at once so I loop through them one by one

Fravadona
  • 13,917
  • 1
  • 23
  • 35