-1

I want to construct a command based upon the number of groups in the yaml file shown in the Example..

Example:

I have groups command as below which extracts the groups from the iamIdentityMappings yaml file,

groups=$(yq read -j generated/identity-mapping.yaml "iamIdentityMappings.[0].groups")

iamIdentityMappings yaml file:

iamIdentityMappings:
- groups:
  - Appdeployer
  - Moregroups
  rolearn: arn:aws:iam::12345:role/eks-project-us-east-1-ppdeployer
  username: user1

Since there are two groups in groups array,I need to add two groups in the below command,

 eksctl create iamidentitymapping --cluster "$name" --region "$region" --arn "$rolearn" --group "Appdeployer" -group "Moregroups" --username "$username"

If there are 3 groups,then --group should be repeated 3 times in the command.

Please let me know how to do this in bash

2

1 Answer 1

0

I have figured out..

grouparr=''
    for group in $groups; do
       echo $group
       if [[ ! $group == '-' ]]; then
          grouparr+=" --group $group "
       fi
    done
Sign up to request clarification or add additional context in comments.

3 Comments

Always quote used variables. Also ${name} syntax is recommended
The for group in $groups part can fail in weird ways if any group names contain spaces (or other whitespace) or anything that can be mistaken for a shell filename wildcard (that includes square brackets).
@Rad4: Instead of putting the values together into a string and rely on word splitting to tear them apart (which would fail if you have spaces in group names), I suggest that you use an array to collect the paramters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.