AWS Cli calls in bash script variable assignment when sending parent-id/child-id

27 views Asked by At

My script needs to get OU parent for an account, Please note I do have a working bash script, with tilde quotes on aws cli commands only when being assigned to a variable, that is exactly same, but for --parentid/--child-id parameters shell check is not needed hence

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
```

The only addition from working script is the assignment of 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

1

There are 1 answers

0
sweez On

This worked for me

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

I had to put the cli command for parent_ou as above, in $();