Bash Script errors when assigning variable as output of AWS CLI command with parent-id/child-id inputs

0

My script needs to get OU parent for an account, when I run corresponding aws cli on terminal, it works fine , I have

aws organizations list-parents --child-id <acctid input> --query Parents[*].Id --output text

However when I use same in bash as below it does not assign the value, process nulls out. bash script has


    for account in $(aws organizations list-accounts --query 'Accounts[].Id' --output text); do
       echo $account
       parent_ou=`aws organizations list-parents --child-id $account --query Parents[*].Id --output text
    echo $parent_ou`

Note it does print account correctly, so its only the parent_ou assignment not working.

Have a similar problem with another, that works on terminal but not within bash, note mocked parent_id

  b_ous = `aws organizations list-organization-units-for-parent --parent-id o_hhhhh_**** --query OrganizationalUnits[*].Id --output text`

The error is

 command "o_hhhhh_****" not found

I have tried using variable instead of hardcode value, its same error.

Any idea what maybe wrong with these variable assignments in bash?

Thanks in advance

3 Antworten
1
Akzeptierte Antwort

Swap those backticks (`) for single quotes ('). Backticks try to execute the command within them, while single quotes just store the literal text. Wrap the entire aws organizations command in $( ) for proper command substitution. It pipes the command output directly into the variable. Assign the output directly to parent_ou within the for loop, like this:

for account in $(aws organizations list-accounts --query 'Accounts[].Id' --output text); do
  echo "$account"
  parent_ou=$(aws organizations list-parents --child-id "$account" --query Parents[*].Id --output text)
  echo "$parent_ou"
done

Consider using command chaining with | for even cleaner code

for account in $(aws organizations list-accounts --query 'Accounts[].Id' --output text); do
  echo "$account"
  parent_ou=$(aws organizations list-parents --child-id "$account" --query Parents[*].Id --output text | tr -d '\n')
  echo "$parent_ou"
done
profile picture
EXPERTE
beantwortet vor 5 Monaten
profile picture
EXPERTE
überprüft vor 5 Monaten
  • You can drop the double quotes around the variables your echoing too

1

Hello,

Your script has 2 points to modify.

b_ous = `aws organizations list-organization-units-for-parent --parent-id o_hhhhh_**** --query OrganizationalUnits[*].Id --output text`
  1. sub command is "list-organizational-units-for-parent"
aws organizations list-organizational-units-for-parent
  1. list-organizational-units-for-parent --parent-id option should handle Root "r-" or OU "ou-", not Organization "o-"
OPTIONS
       --parent-id (string)
          The unique identifier (ID) of the root or OU whose child OUs you
          want to list.

          The regex pattern for a parent ID string requires one of the
          following:

          o Root - A string that begins with "r-" followed by from 4 to 32
            lowercase letters or digits.

          o Organizational unit (OU) - A string that begins with "ou-"
            followed by from 4 to 32 lowercase letters or digits (the ID of
            the root that the OU is in). This string is followed by a second
            "-" dash and from 8 to 32 additional lowercase letters or digits.
mitaoki
beantwortet vor 5 Monaten
0

Hello.

I executed the shell script below using CloudShell and it was executed successfully.
I can't reproduce the same error as you.
Is it possible for you to share the entire shell script?

#!/bin/bash

for account in $(aws organizations list-accounts --query 'Accounts[].Id' --output text); do
        echo $account
        parent_ou=`aws organizations list-parents --child-id $account --query Parents[*].Id --output text`
        echo $parent_ou
done
profile picture
EXPERTE
beantwortet vor 5 Monaten
  • strange. Yes, the exact command works fine on my terminal, just the bash assignment fails. Thanks for for taking a look.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen